(4.1.36.11)Android 一张图理解getWidth和getMeasuredWidth
2016-03-24 16:34
656 查看
view.getMeasuredHeight()可能隐藏在上头.可能在下头.如果view没有超出屏幕的时候view.getMeasuredHeight() 有可能小于 view.getHeight()
getWidth():
View在設定好佈局後整個View的寬度。
getMeasuredWidth():
對View上的內容進行測量後得到的View內容佔據的寬度
Google文档的英文说明:
getWidth():Return the width of the your view.
Returns: the width of your view, in pixels
getMeasuredWidth():
The width of this view as measured in the most recent call to measure(). This should be used during measurement and layout calculations only. Use getWidth() to see how wide
a view is after layout.
Returns: the measured width of this view
前提知识点:
1. 在一个类初始化时,即在构造函数当中是得不到View的实际大小的(这个我测试过,的确)。大家可以试试,getWidth()和getMeasuredWidth()得到的结果都是0,但是可以从onDraw()方法或者dispatchDraw()方法里面获得。可以通过调用invalidate()来执行onDraw()和dispatchDraw()方法。2. 这两个方法所得到的结果的单位是像素即pixel
正确的理解:
getWidth(): View在设定好布局后,整个View的宽度getMeasuredWidth():对View上的内容进行测量后得到的View内容占据的宽度。前提是你必须在父布局的onLayout()方法或者此View的onDraw()方法里调用measure(0,0);(measure参数的值可以知己定义),否则得到的结果和getWidth()得到的结果是一样的。
这两个方法最主要的区别在于,是否使用了measure()方法,同时measure()使用的位置也很重要。
getHeight() 和 get MeasuredHeight() 区别同理。
相关文章推荐
- (4.1.36.10)[Android]Android字体高度的研究
- Android调试驱动抓log的方法
- Android中Volley框架Get,POST封装使用及自动解析JSON
- 通常在使用Android的getActionBar()的时候报空指针异常,通常有5种原因。
- Android系统关机或重启的几种实现方式
- android 数据传递——实例化对象调用方法
- Android图片处理-相机、相处简单调用
- ListView和GridView与ScrollView冲突只显示一行的问题
- Android中退出多个Activity应用
- android activity onWindowFocusChanged函数的使用场景
- 自定义ViewGroup,流式布局FlowLayout
- android 高效加载大图
- Android 特色开发,基于位置的服务
- Android异常和工具使用笔记
- AndroidStudio使用常见问题(实时更新)
- Android 学习笔记(1) —— TextView
- Android 学习笔记(2) —— EditView
- android手机推送消息 (百度云推送)
- 很全的demo,包括调用系统相机全屏拍照并保存,访问系统相册,多项保存照片。
- 如何在Android Studio中添加多个项目