View的onMeasure方法
2016-04-18 17:44
260 查看
View.MeasureSpec类中的常量:
由onMeasure方法带的参数widthMeasureSpec、heightMeasureSpec,得到View的尺寸的规格和大小:
实例代码
private static final int MODE_SHIFT = 30; public static final int UNSPECIFIED = 0 << MODE_SHIFT;//0 public static final int EXACTLY = 1 << MODE_SHIFT;//1073741824 public static final int AT_MOST = 2 << MODE_SHIFT;//-2147483648
由onMeasure方法带的参数widthMeasureSpec、heightMeasureSpec,得到View的尺寸的规格和大小:
int specMode = MeasureSpec.getMode(measureSpec);//UNSPECIFIED,EXACTLY,AT_MOST int specSize = MeasureSpec.getSize(measureSpec);
实例代码
public class TestView extends View { public TestView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } public TestView(Context context) { super(context); // TODO Auto-generated constructor stub } @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { Log.v("view.onMeasure", "widthMeasureSpec="+widthMeasureSpec); Log.v("view.onMeasure", "heightMeasureSpec="+heightMeasureSpec); int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); Log.v("view.onMeasure", "widthMode="+widthMode); Log.v("view.onMeasure", "heightMode="+heightMode); Log.v("view.onMeasure", "widthSize="+widthSize); Log.v("view.onMeasure", "heightSize="+heightSize); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.viewlearning.MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <com.example.viewlearning.TestView android:layout_width="50dp" android:layout_height="50dp" android:background="#666666"/> </LinearLayout>
相关文章推荐
- Java程序的路径问题
- 87.直接向对象发送消息和通过performSelector调用的区别
- java 连接sql server 2008 数据库 笔记
- 简单制作漂亮的照片拼贴文字效果
- iOS大牛博客集
- Jquery如何获得<iframe>嵌套页面中的元素
- eclipse重定向输入输出到文件
- jQuery 序列化表单数据 serialize() serializeArray()
- Windows上使用ionic开发IOS程序
- css选择器
- Java多线程面试问题集锦
- 使用Profiler工具分析内存占用情况
- 【转】android Looper 理解
- Nginx_查看并发连接数
- [Python] 函数基本
- centos上传或下载文件
- VS获取工程版本信息,AxtiveX或者OCX的版本取得
- java获取当前时间
- 利用JS提交表单的几种方法和验证
- 从零迈向iOS--新手篇