android getWidth()和getMeasuredWidth()方法的区别
getWidth()
Return the width of the your view.
Returns
- The width of your view, in pixels.
源代码:
public final int getWidth() {
return mRight - mLeft;
}
getwidth返回的是右边坐标减轻坐标减去左边坐标,这要在布局之后才能确定它们的坐标,也就是说在布局后才能调用getwidth来获取。所以getWidth()获得的宽度是View在设定好布局后整个View的宽度。
getMeasuredWidth()
Return the full width measurement information for this view as computed by the most recent call to measure(int, int). This result is a bit mask as defined byMEASURED_SIZE_MASK and MEASURED_STATE_TOO_SMALL. 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 as a bit mask.
得到的是最近一次调用measure()方法测量后得到的是View的宽度,它应该仅仅用在测量和Layout的计算中。再看源码:
public final int getMeasuredWidth() {
return mMeasuredWidth & MEASURED_SIZE_MASK;
}
return The raw measured width of this view 获得的是原始的测量宽度。所以说getMeasuredWidth()是对View上的内容进行测量后得到的View内容占据的宽度。前提是你必须在父布局的onLayout()方法或者此View的onDraw()方法里调用measure(0,0);(measure中的参数的值你自己可以定义),否则你得到的结果和getWidth()得到的结果是一样的。
区别
getMeasuredWidth:对View上的内容进行测量后得到的View内容占据的宽度的。
getWidth:View在设定号布局后整个View的宽度。
使用场合
getMeasuredWidth:在自定义view重写onLayout时、在我们用layoutinflater动态加载view后想获得view的原始宽度时。
getWidth:一般在view已经布局后呈现出来了,想获取宽度时。
getMeasuredWidth(): 對View上的內容進行測量後得到的View內容佔據的寬度
android getWidth()和getMeasuredWidth()方法的区别的更多相关文章
- android中getWidth()和getMeasuredWidth()之间的区别
先给出一个结论:getMeasuredWidth()获取的是view原始的大小,也就是这个view在XML文件中配置或者是代码中设置的大小.getWidth()获取的是这个view最终显示的大小,这个 ...
- Android getWidth和getMeasuredWidth的区别
getWidth 得到的事某个View的实际尺寸. getMeasuredWidth 得到的是某个View想要在parent view里面占的大小 相比你也见过这样的解释,听起来这样的解释也是云里雾里 ...
- Android getWidth和getMeasuredWidth
1. 在一个类初始化时,即在构造函数当中我们是得不到View的实际大小的.感兴趣的朋友可以试一下,getWidth()和getMeasuredWidth()得到的结果都是0.但是我们可以从onDraw ...
- Android -- getWidth()与getMeasuredWidth()
getWidth() Return the width of the your view. Returns The width of your view, in pixels. 源代码: public ...
- getWidth()和getMeasuredWidth()的区别
结论:getMeasuredWidth()获取的是view原始的大小,也就是这个view在XML文件中配置或者是代码中设置的大小.getWidth()获取的是这个view最终显示的大小,这个大小有可能 ...
- Android中View窗口getWidth和getMeasuredWidth的差别
今天在研究自己定义listview的下拉刷新的效果.想移植到项目需求中,再看自己定义源代码时发现了一个问题就是getWidth和getMeasuredWidth两个方法有什么差别,求教万能的百度,经调 ...
- (转)Android之常用功能方法大集合
这些,都是Andorid中比较常用的方法和功能,在网上搜集整理一下记录之,以备不时之需.由于经过多次转载,源文作者不确凿,在此申明,敬请见谅.不得不赞,非常实用. 1.判断sd卡是否存在 boolea ...
- View inflate方法和LayoutInflater inflate方法的区别详解
原创文章,转载请注明出处:http://www.cnblogs.com/baipengzhan/p/6257510.html 我们在Android开发中,对于将布局填充成View对象,最常用的两种办法 ...
- Android中style和theme的区别
在学习Xamarin android的过程中,最先开始学习的还是熟练掌握android的六大布局-LinearLayout .RelativeLayout.TableLayout.FrameLayou ...
随机推荐
- Alpha版本项目展示得分
团队名称 得分 newbe 80 C705 100 ourteam 60 sevens 50 sixsix 190 dxteam 75 hots 200 Echo 90
- Linux内核分析作业三
构造一个简单的Linux系统MenuOS 复习 计算机三大法宝 存储程序计算机 函数调用堆栈 中断 操作系统两把宝剑 中断上下文的切换 进程上下文的切换 一.Linux内核源代码简介 函数目录 Lin ...
- 20135327郭皓——Linux内核分析第二周 操作系统是如何工作的
操作系统是如何工作的 上章重点回顾: 计算机是如何工作的?(总结)——三个法宝 存储程序计算机工作模型,计算机系统最最基础性的逻辑结构: 函数调用堆栈,高级语言得以运行的基础,只有机器语言和汇编语言的 ...
- Alpha 冲刺五
团队成员 051601135 岳冠宇 051604103 陈思孝 031602629 刘意晗 031602248 郑智文 031602234 王淇 会议照片 项目燃尽图 项目进展 暂无实质性进展. 项 ...
- PAT 甲级 1096 Consecutive Factors
https://pintia.cn/problem-sets/994805342720868352/problems/994805370650738688 Among all the factors ...
- Tour HDU - 3488(最大权值匹配)
Tour In the kingdom of Henryy, there are N (2 <= N <= 200) cities, with M (M <= 30000) one- ...
- 解决 Previous operation has not finihsed; run ‘cleanup’ if it was interrupted Please execute the ‘Cleanup’ command
更新时遇到这个问题,解决方法如下: 把根目录下的.svn目录删除掉,再checkout,然后就会出现下面的加version的action. 疯吻IT
- SP422 TRANSP2 - Transposing is Even More Fun——置换群+反演
挺神仙的置换题 SP422 TRANSP2 - Transposing is Even More Fun 这个博客除了开始举例子别的都是对的: https://blog.csdn.net/Braket ...
- ASP.NET MVC验证框架中关于属性标记的通用扩展方法
http://www.cnblogs.com/wlb/archive/2009/12/01/1614209.html 之前写过一篇文章<ASP.NET MVC中的验证>,唯一的遗憾就是在使 ...
- Python 基础数据类型之set
set是一个无序且不重复的元素集合,相当于字典的键,不重复,不可变 一.set变量初始化 A = set() #注意在创建空集合的时候只能使用s=set(),因为s={}创建的是空字典 B = {&q ...