getWidth和getMeasuredWidth在何时可以得到正确数值

getMeasuredWidth在源码中的解释如下:

    /**
     * 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 full width measurement information for this view as computed
     * by the most recent call to {@link #measure(int, int)}.  This result is a bit mask
     * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
     * This should be used during measurement and layout calculations only. Use
     * {@link #getWidth()} to see how wide a view is after layout.
     *
     * @return The measured width of this view as a bit mask.
     */
    public final int getMeasuredWidthAndState() {
        return mMeasuredWidth;
    }

大致意思也就是说返回最后一次调用onMeasure所测量得到的宽度。

getWidth在源码中返回View的宽度 单位是pixels

/**
     * 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;
    }

写一个Demo测试一下 代码如下:

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		MLinearLayout root = new MLinearLayout(this);
		setContentView(root);
	}

	class MLinearLayout extends LinearLayout
	{

		public MLinearLayout(Context context)
		{
			super(context);
			setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.MATCH_PARENT));
			addView(new MTextView(context));
			Log.i("AAA","LinearLayout MLinearLayout Width:"+getWidth() +" Height:"+getHeight());
			Log.i("AAA","LinearLayout MLinearLayout MeasuredWidth:"+getMeasuredWidth() +" MeasuredHeight:"+getMeasuredHeight());
		}

		@Override
		protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
			// TODO Auto-generated method stub
			super.onMeasure(widthMeasureSpec, heightMeasureSpec);
			Log.i("AAA","LinearLayout onMeasure Width:"+getWidth() +" Height:"+getHeight());
			Log.i("AAA","LinearLayout onMeasure MeasuredWidth:"+getMeasuredWidth() +" MeasuredHeight:"+getMeasuredHeight());
		}

		@Override
		protected void onLayout(boolean changed, int l, int t, int r, int b) {
			// TODO Auto-generated method stub
			super.onLayout(changed, l, t, r, b);
			Log.i("AAA","LinearLayout onLayout Width:"+getWidth() +" Height:"+getHeight());
			Log.i("AAA","LinearLayout onLayout MeasuredWidth:"+getMeasuredWidth() +" MeasuredHeight:"+getMeasuredHeight());
		}

	}

	class MTextView extends TextView
	{

		public MTextView(Context context) {
			super(context);
			setLayoutParams(new LayoutParams(200, 300));
			setText("测试");
			Log.i("AAA"," TextView MTextView Width:"+getWidth() +" Height:"+getHeight());
			Log.i("AAA"," TextView MTextView MeasuredWidth:"+getMeasuredWidth() +" MeasuredHeight:"+getMeasuredHeight());
		}

		@Override
		protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
			// TODO Auto-generated method stub
			super.onMeasure(widthMeasureSpec, heightMeasureSpec);
			Log.i("AAA"," TextView onMeasure Width:"+getWidth() +" Height:"+getHeight());
			Log.i("AAA"," TextView onMeasure MeasuredWidth:"+getMeasuredWidth() +" MeasuredHeight:"+getMeasuredHeight());
		}

		@Override
		protected void onLayout(boolean changed, int left, int top, int right,
				int bottom) {
			// TODO Auto-generated method stub
			super.onLayout(changed, left, top, right, bottom);
			Log.i("AAA"," TextView onLayout Width:"+getWidth() +" Height:"+getHeight());
			Log.i("AAA"," TextView onLayout MeasuredWidth:"+getMeasuredWidth() +" MeasuredHeight:"+getMeasuredHeight());
		}
	}
}

运行之后得到的结果如下:

I/AAA     ( 3631):  TextView MTextView Width:0 Height:0

I/AAA     ( 3631):  TextView MTextView MeasuredWidth:0 MeasuredHeight:0

I/AAA     ( 3631): LinearLayout MLinearLayout Width:0 Height:0

I/AAA     ( 3631): LinearLayout MLinearLayout MeasuredWidth:0 MeasuredHeight:0

I/AAA     ( 3631):  TextView onMeasure Width:0 Height:0

I/AAA     ( 3631):  TextView onMeasure MeasuredWidth:200 MeasuredHeight:300

I/AAA     ( 3631): LinearLayout onMeasure Width:0 Height:0

I/AAA     ( 3631): LinearLayout onMeasure MeasuredWidth:1024 MeasuredHeight:502

I/AAA     ( 3631):  TextView onLayout Width:200 Height:300

I/AAA     ( 3631):  TextView onLayout MeasuredWidth:200 MeasuredHeight:300

I/AAA     ( 3631): LinearLayout onLayout Width:1024 Height:502

I/AAA     ( 3631): LinearLayout onLayout MeasuredWidth:1024 MeasuredHeight:502

I/AAA     ( 3631):  TextView onMeasure Width:200 Height:300

I/AAA     ( 3631):  TextView onMeasure MeasuredWidth:200 MeasuredHeight:300

I/AAA     ( 3631): LinearLayout onMeasure Width:1024 Height:502

I/AAA     ( 3631): LinearLayout onMeasure MeasuredWidth:1024 MeasuredHeight:502

I/AAA     ( 3631):  TextView onLayout Width:200 Height:300

I/AAA     ( 3631):  TextView onLayout MeasuredWidth:200 MeasuredHeight:300

I/AAA     ( 3631): LinearLayout onLayout Width:1024 Height:502

I/AAA     ( 3631): LinearLayout onLayout MeasuredWidth:1024 MeasuredHeight:502

结论:

1.在构造方法中无论是getWidth还是getMeasuredWidth都是得不到正确数值的。

2.getMeasuredWidth得到正确数值需要在调用过onMeasure之后。

3.getWidth得到正确数值需要调用过onLayout之后。

VIew的绘制过程:http://www.cnblogs.com/cowboybusy/archive/2012/08/26/2718888.html

getWidth和getMeasuredWidth在何时可以得到正确数值

时间: 2024-11-14 03:14:57

getWidth和getMeasuredWidth在何时可以得到正确数值的相关文章

Android getWidth和getMeasuredWidth

1. 在一个类初始化时,即在构造函数当中我们是得不到View的实际大小的.感兴趣的朋友可以试一下,getWidth()和getMeasuredWidth()得到的结果都是0.但是我们可以从onDraw()方法里面的到控件的大小. 2.这两个所得到的结果的单位是像素即pixel. getWidth(): 得到的是view在父Layout中布局好后的宽度值,如果没有父布局,那么默认的父布局就是整个屏幕. getMeasuredWidth():先看一下API里面是怎么说的.The width of t

Android中View窗口getWidth和getMeasuredWidth的差别

今天在研究自己定义listview的下拉刷新的效果.想移植到项目需求中,再看自己定义源代码时发现了一个问题就是getWidth和getMeasuredWidth两个方法有什么差别,求教万能的百度,经调研发现这两个方法的不同点是,getWidth获得的是当前View的可视的宽度,可是向下拉刷新这种需求,那个头部提示的View是隐藏起来的这样用getWidth就获取不到View的宽度了.所以採取getMeasuredWidth的方式获得. getMeasuredWidth的方法代表了能够获取View

Android中View窗体getWidth和getMeasuredWidth的区别

今天在研究自定义listview的下拉刷新的效果,想移植到项目需求中,再看自定义源码时发现了一个问题就是getWidth和getMeasuredWidth两个方法有什么区别,求教万能的百度,经调研发现这两个方法的不同点是,getWidth获得的是当前View的可视的宽度,但是向下拉刷新这样的需求,那个头部提示的View是隐藏起来的这样用getWidth就获取不到View的宽度了,所以采取getMeasuredWidth的方式获得.getMeasuredWidth的方法代表了可以获取View的可视

【Android】getwidth和getmeasuredwidth的区别以及两者的使用场景

首先,看getWidth()的官方说明: public final int getWidth () Added in API level 1 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

android中getWidth()和getMeasuredWidth()之间的区别

先给出一个结论:getMeasuredWidth()获取的是view原始的大小,也就是这个view在XML文件中配置或者是代码中设置的大小.getWidth()获取的是这个view最终显示的大小,这个大小有可能等于原始的大小也有可能不等于原始大小. 从源码上开始分析一下这两个方法的区别.首先来看一下getMeasuredWidth()这个方法. 1 public final int getMeasuredWidth() { 2 return mMeasuredWidth & MEASURED_S

getwidth和getmeasuredwidth的区别以及两者的使用场景

getWidth()获得的宽度是View在设定好布局后整个View的宽度 getMeasuredWidth()是对View上的内容进行测量后得到的View内容占据的宽度. getWidth得到是某个view的实际尺寸. getMeasuredWidth是得到某view想要在parent view里面占的大小. 两者的使用场合: getMeasuredWidth:在自定义view重写onLayout时.在我们用layoutinflater动态加载view后想获得view的原始宽度时. getWid

JavaScript判断是否是正确数值 isNaN

NaN在JavaScript中表示不是数字 JavaScript中isNaN函数方法是返回一个 Boolean 值,指明提供的值是否是保留值 NaN (不是数字). 使用方法:isNaN(numValue)其中必选项 numvalue 参数为要检查是否为 NAN 的值. 如果值是 NaN, 那么 isNaN 函数返回 true ,否则返回 false . 使用这个函数的典型情况是检查 parseInt 和 parseFloat 方法的返回值.还有一种办法,变量可以与它自身进行比较. 如果比较的结

【转】Android中View的绘制过程 onMeasure方法简述 附有自定义View例子

Android中View的绘制过程 当Activity获得焦点时,它将被要求绘制自己的布局,Android framework将会处理绘制过程,Activity只需提供它的布局的根节点. 绘制过程从布局的根节点开始,从根节点开始测量和绘制整个layout tree. 每一个ViewGroup 负责要求它的每一个孩子被绘制,每一个View负责绘制自己. 因为整个树是按顺序遍历的,所以父节点会先被绘制,而兄弟节点会按照它们在树中出现的顺序被绘制. 绘制是一个两遍(two pass)的过程:一个mea

Android View系统解析(下)

转载请注明出处:http://blog.csdn.net/singwhatiwanna/article/details/38426471(来自singwhatiwanna的csdn博客) Android View系统解析系列: Android View系统解析(上) 介绍View的基础知识.View的滑动.弹性滑动.滑动冲突解决方式.事件分发等 Android View系统解析(下) 介绍View的Framework层原理.View的measure / layout / draw三大流程和一些高