Android getWidth和getMeasuredWidth的正解

原文摘自: http://gundumw100.iteye.com/blog/1025191

解释一下 之所以用翻译是因为我自己觉得看着繁体字可难受。。为了不让大家难受。。就翻译了。。哈哈

一、 也许很多同学对getWidth()和getMeasuredWidth()的用法有很多的不解,这两者之间有什么样的不同呢,网上也有各种不同的版本,但大多都大同小异罢了,从这个地方CTRL + C 到另一个地方CTRL + V, 没有把问题说透,也有一部分文章误导了大家对这两个方法的认识,我也是深受其害。这里先纠正下面的一个版本,Baidu上一搜一大堆的,可惜这种说法是错的,所以希望大家就不要再盲目的转载到你的空间里:

getWidth 得到的事某个View的实际尺寸。

getMeasuredWidth 得到的是某个View想要在parent view里面占的大小

相比你也见过这样的解释,听起来这样的解释也是云里雾里,没有把问题点透。

二、好了,错误的版本不多说了,下面对这两个方法做一下正解,首先大家应先知道一下几点:

1. 在一个类初始化时,即在构造函数当中我们是得不到View的实际大小的。感兴趣的朋友可以试一下,getWidth()和getMeasuredWidth()得到的结果都是0.但是我们可以从onDraw()方法里面的到控件的大小。

2.这两个所得到的结果的单位是像素即pixel。

  对这两个方法做介绍:

  getWidth(): 得到的是view在父Layout中布局好后的宽度值,如果没有父布局,那么默认的父布局就是真个屏幕。也许不好理解通过一个例子来说明一下:

[java]  view plain copy
  1. public class Test extends Activity {  
  2.  private LinearLayout mBackgroundLayout;  
  3.  private TextViewTest mTextViewTest;  
  4.   
  5.  /** Called when the activity is first created. */  
  6.  @Override  
  7.  public void onCreate(Bundle savedInstanceState) {  
  8.   super.onCreate(savedInstanceState);  
  9.   
  10.   mBackgroundLayout = new MyLayout(this);  
  11.   mBackgroundLayout.setLayoutParams(new LinearLayout.LayoutParams(  
  12.     LinearLayout.LayoutParams.FILL_PARENT,  
  13.     LinearLayout.LayoutParams.FILL_PARENT));  
  14.   
  15.   mTextViewTest = new TextViewTest(this);  
  16.   
  17.   mBackgroundLayout.addView(mTextViewTest);  
  18.   setContentView(mBackgroundLayout);  
  19.  }  
  20.  public class MyLayout extends LinearLayout{  
  21.   
  22.   public MyLayout(Context context) {  
  23.    super(context);  
  24.    // TODO Auto-generated constructor stub  
  25.   }  
  26.   
  27.   @Override  
  28.   protected void onLayout(boolean changed, int l, int t, int r, int b) {  
  29.    // TODO Auto-generated method stub  
  30.    super.onLayout(changed, l, t, r, b);  
  31.    Log.i("Tag""--------------");  
  32.    View mView=getChildAt(0);  
  33.    mView.measure(00);  
  34.   }  
  35.     
  36.  }  
  37.  public class TextViewTest extends TextView {  
  38.   public TextViewTest(Context context) {  
  39.    super(context);  
  40.    // TODO Auto-generated constructor stub  
  41.    setText("test test ");  
  42.   }  
  43.     
  44.   @Override  
  45.    protected void onDraw(Canvas canvas) {  
  46.    // TODO Auto-generated method stub  
  47.    super.onDraw(canvas);  
  48.    // measure(0, 0);  
  49.    Log.i("Tag""width: " + getWidth() + ",height: " + getHeight());  
  50.    Log.i("Tag""MeasuredWidth: " + getMeasuredWidth()  
  51.      + ",MeasuredHeight: " + getMeasuredHeight());  
  52.    }  
  53.   
  54.  }  
  55. }  

这里是在LinearLayout里添加的一个TextView控件,如果此时要得到对TextView获得getWidth(),那么是在TextView添加到Layout后再去获取值,并不单单的是对TextView本身宽度的获取。

getMeasuredWidth():先看一下API里面是怎么说的。
The width of this view as measured in the most recent call to measure(). This should be used during measurement and layout calculations only.

得到的是最近一次调用measure()方法测量后得到的是View的宽度,它仅仅用在测量和Layout的计算中。

所以此方法得到的是View的内容占据的实际宽度。

你如果想从一个简单的例子中得到他们的不同,下面将对上面的例子做一下修改。

[java]  view plain copy
  1. public class Test extends Activity {  
  2.  private TextViewTest mTextViewTest;  
  3.   
  4.  /** Called when the activity is first created. */  
  5.  @Override  
  6.  public void onCreate(Bundle savedInstanceState) {  
  7.   super.onCreate(savedInstanceState);  
  8.   mTextViewTest = new TextViewTest(this);  
  9.   setContentView(mTextViewTest);  
  10.  }  
  11.   
  12.  public class TextViewTest extends TextView {  
  13.   public TextViewTest(Context context) {  
  14.    super(context);  
  15.    // TODO Auto-generated constructor stub  
  16.    setText("test test ");  
  17.   }  
  18.   
  19.   @Override  
  20.   protected void onDraw(Canvas canvas) {  
  21.    // TODO Auto-generated method stub  
  22.    super.onDraw(canvas);  
  23.    measure(00);  
  24.    Log.i("Tag""width: " + getWidth() + ",height: " + getHeight());  
  25.    Log.i("Tag""MeasuredWidth: " + getMeasuredWidth()  
  26.      + ",MeasuredHeight: " + getMeasuredHeight());  
  27.   }  
  28.  }  
  29. }  

总结(正解):

getWidth(): View在设定好布局后整个View的宽度。

getMeasuredWidth(): 对View上的内容进行测量后得到的View内容占据的宽度,前提是你必须在父布局的onLayout()方法或者此View的onDraw()方法里调用measure(0,0);(measure中的参数的值你自己可以定义),否则你得到的结果和getWidth()得到的结果是一样的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值