自定义View-onMeasure()方法
2015-09-18 14:06
357 查看
在android自定义View的时候经常需要复写View的onMeasure()方法,但是有些时候却并不需要。了解其中的原因有助于我们深刻掌握自定义view。
首先看一下View中onMeasure()方法的官方解释:
大概意思就是该方法会测量View,从而决定View的大小,需要我们复写它。光这几句话很难明白其真正的用法。
当没有复写onMeasuer()方法时,我们使用march_parent和wrap_content效果一样,都是填充父控件。所以当我们自定义的View想自己定义大小,而又使用wrap_content时就需要复写onMeasure()。在复写的onMeasure()里根据你的需求重新的定义View的大小,然后通过:
生效。
首先看一下View中onMeasure()方法的官方解释:
Measure the view and its content to determine the measured width and the measured height. This method is invoked by {@link #measure(int, int)} and should be overridden by subclasses to provide accurate and efficient measurement of their contents.
大概意思就是该方法会测量View,从而决定View的大小,需要我们复写它。光这几句话很难明白其真正的用法。
当没有复写onMeasuer()方法时,我们使用march_parent和wrap_content效果一样,都是填充父控件。所以当我们自定义的View想自己定义大小,而又使用wrap_content时就需要复写onMeasure()。在复写的onMeasure()里根据你的需求重新的定义View的大小,然后通过:
setMeasuredDimension(measuredWidth, measuredHeight);
生效。
相关文章推荐
- IOS navigationController.popToViewController的使用
- greenDao的使用--- 配置 androidstudio
- Android多种方式实现自定义Dialog对话框
- C# ListView用法详解
- ajax传值
- App推送java服务端_jpush激光推送
- JSONValue.parse出错
- JDK和JRE的区别及各自的作用
- Android 仿QQ首页ListView左滑置顶、删除等实现源码
- 顶点突然增加
- Android(Lollipop/5.0) Material Design简介
- kitkat-s5p4418drone 记录
- Android在layout xml中使用include[转]
- Android可移动控件
- 二人结对编程项目+电梯算法
- 指定IE兼容模式的代码
- javaee后台适合用的编辑器插件
- 第四章 语句和声明
- 飞鸽传书源码分析三-网络
- HttpClient使用post方式模拟表单提交数据到服务器并下载服务器文件