自定义view onMeasure方法的重写
来源:互联网 发布:js json对象元素个数 编辑:程序博客网 时间:2024/05/17 20:31
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自定义view onMeasure用法 (MeasureDemoActivity)"/> <phoebe.frame.view.CustomView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/holo_red_light"/></LinearLayout>
@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {String widthSpec = MeasureSpec.toString(widthMeasureSpec);String heightSpec = MeasureSpec.toString(heightMeasureSpec);Log.d("measure_spec", widthSpec + " --- " + heightSpec);super.onMeasure(widthMeasureSpec, heightMeasureSpec);Log.d("measure_spec", getMeasuredWidth() + " " + getMeasuredHeight());}
02-21 19:07:23.740: D/measure_spec(19713): MeasureSpec: EXACTLY 720 MeasureSpec: AT_MOST 114302-21 19:07:23.740: D/measure_spec(19713): 720 1143
view是充满整个layout页面。很显然这并不是我们想要的效果
如果要实现设置wrap_content以后view的宽高刚好是view content的宽高,则需要重写onMeasure
(至于怎么重写,先不讨论、、可以参考 TextView的源码)
另外摘一段网上的代码
http://stackoverflow.com/questions/12266899/onmeasure-custom-view-explanation
@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int desiredWidth = 100; int desiredHeight = 100; int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); int width; int height; //Measure Width if (widthMode == MeasureSpec.EXACTLY) { //Must be this size width = widthSize; } else if (widthMode == MeasureSpec.AT_MOST) { //Can't be bigger than... width = Math.min(desiredWidth, widthSize); } else { //Be whatever you want width = desiredWidth; } //Measure Height if (heightMode == MeasureSpec.EXACTLY) { //Must be this size height = heightSize; } else if (heightMode == MeasureSpec.AT_MOST) { //Can't be bigger than... height = Math.min(desiredHeight, heightSize); } else { //Be whatever you want height = desiredHeight; } //MUST CALL THIS setMeasuredDimension(width, height);}
0 0
- 自定义view onMeasure方法的重写
- 自定义view 重写onMeasure()方法
- 重写View的onMeasure方法
- 自定义View-2-重写onMeasure
- 自定义View中为什么需要重写onMeasure()方法?
- 自定义View的onMeasure方法理解
- 自定义view,viewgroup的onMeasure 方法
- Android 自定义View onMeasure方法的实现
- 自定义View的onMeasure()
- 自定义View-onMeasure()方法
- 自定义View之onMeasure方法
- 自定义View之onMeasure()方法
- android 自定义View之View的测量(onMeasure()方法)
- 自定义View(三)之View类的onMeasure方法详解
- android 自定义View之View的测量(onMeasure()方法)
- View的onMeasure方法
- View 的onMeasure方法
- View的onMeasure方法
- Leetcode 258
- c++中new与delete
- sort c++ double
- Android JNI开发笔记
- 全局函数和成员函数
- 自定义view onMeasure方法的重写
- 淘宝应对"双11"的技术架构分析
- 友元函数与友元类
- 职坐标 选课系统整体框架 参考
- 运算符重载
- Apache配置支持markdown格式
- 继承
- os_task.c
- HDU-1039