getWidth和getMeasuredWidth

来源:互联网 发布:淘宝导购网站 编辑:程序博客网 时间:2024/05/20 05:54
一。也許很多童鞋對getWidth()和getMeasuredWidth()的用法有很多的不解,這兩者之間有什麼樣的不同呢,網上也有各種不同的版本,但大多數都大同小異,從這個地方Ctrl+C,到另一個地方Ctrl+V,沒有把問題說透,也有一部分文章誤導了大家對這兩個方法的認識,我也是深受其害。這裡先糾正下面的一個版本的說法,Baidu上一搜一大堆的,可惜這種說法是錯的,所以希望大家就不要再盲目的轉載到你的空間裡: 
                      getWidth得到是某个view的实际尺寸. 
                      getMeasuredWidth是得到某view想要在parent view里面占的大小. 
想必你也見過這樣的解釋,聽起來這樣的解釋也似雲裡霧裡,沒有把問題點透。 

二。好了,錯誤的版本就不過多說了,下面對這兩個方法做一下正解,首先大家應先知道以下幾點: 
1. 在一個類初始化時,即在構造函數當中我們是得不到View的實際大小的。感興趣的朋友可以試一下,getWidth()和getMeasuredWidth()得到的結果都是0.但是我們可以從onDraw()方法裡面得到控件的大小。 
2. 這兩個方法所得到的結果的單位是像素即pixel. 
對兩個方法做介紹: 
getWidth(): 得到的是view在父Layout中佈局好後的寬度值,如果沒有父佈局,那麼默認的父佈局是整個屏幕。也許不好理解。通過一個例子來說明一下。 
例1 : 
Java代码  收藏代码
  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代码  收藏代码
  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()得到的結果一樣。 
       也許我組織的不是很好,大家有什麼不清楚的地方再給我留言。關於這兩個方法的區別就是看你有沒有用measure()方法,當然measure()的位置也是很重要的。 
      转载自http://gundumw100.iteye.com/blog/1025191
0 0
原创粉丝点击