【Android】getwidth和getmeasuredwidth的区别以及两者的使用场景
来源:互联网 发布:ip mac 扫描工具 编辑:程序博客网 时间:2024/05/20 09:43
首先,看getWidth()的官方说明:
public final int getWidth ()
Return the width of the your view.
Returns
- The width of your view, in pixels.
返回view的宽度,说的不详细,再看getWidth源码:
<span style="font-size:18px;"> /** * Return the width of the your view. * * @return The width of your view, in pixels. */ @ViewDebug.ExportedProperty(category = "layout") public final int getWidth() { return mRight - mLeft; }</span>
从源码可以看出,getwidth返回的是右边坐标减轻坐标减去左边坐标,这要在布局之后才能确定它们的坐标,也就是说在布局后才能调用getwidth来获取。所以getWidth()获得的宽度是View在设定好布局后整个View的宽度。
而对于getmeasuredwidth,
public final int getMeasuredWidthAndState ()
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.
/** * Like {@link #getMeasuredWidthAndState()}, but only returns the * raw width component (that is the result is masked by * {@link #MEASURED_SIZE_MASK}). * * @return The raw measured width of this view. */ 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重写onLayout时、在我们用layoutinflater动态加载view后想获得view的原始宽度时。
getWidth:一般在view已经布局后呈现出来了,想获取宽度时
- 【Android】getwidth和getmeasuredwidth的区别以及两者的使用场景
- 【Android】getwidth和getmeasuredwidth的区别以及两者的使用场景
- 【Android】getwidth和getmeasuredwidth的区别以及两者的使用场景
- Android中getWidth和getMeasuredWidth的区别
- Android getWidth和getMeasuredWidth的区别
- Android中getwidth和getmeasuredwidth的区别
- getWidth() 和 getMeasuredWidth()的区别
- getwidth和getmeasuredwidth的区别
- getWidth()和getMeasuredWidth()的区别
- getMeasuredWidth()和getWidth() 的区别
- Android中View窗体getWidth和getMeasuredWidth的区别
- android中getWidth()和getMeasuredWidth()之间的区别
- android中getWidth()和getMeasuredWidth()之间的区别
- android中getWidth()和getMeasuredWidth()之间的区别
- Android getWidth和getMeasuredWidth 区别
- Android getWidth和getMeasuredWidth 区别
- Android getWidth和getMeasuredWidth 区别
- Android getWidth和getMeasuredWidth 区别
- 哎呀头疼的驱动
- zoj 3811||牡丹江网赛 c题 并查集
- Android应用开发-小巫CSDN博客客户端开发开篇
- 开源HTML5 APP开发神器CanTK发布
- 标签管理组织高层领导了解标签进销存情况是不是所谓的管理型业务用例
- 【Android】getwidth和getmeasuredwidth的区别以及两者的使用场景
- codeigniter读取数据库的公共配置并全局缓存的实现方案
- TimeChooser
- Cocos2d-x-Lua 开发简单的小游戏(记数字踩白块)
- Python 对文档内容TFIDF处理
- STM32L-Power control(PWR)
- STM32L-Power control(PWR)
- MVC一个入门的例子(二)
- Reactor模式和NIO