AndroidgetWidth和getMeasuredWidth的正解.doc_第1页
AndroidgetWidth和getMeasuredWidth的正解.doc_第2页
AndroidgetWidth和getMeasuredWidth的正解.doc_第3页
AndroidgetWidth和getMeasuredWidth的正解.doc_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

一。也許很多童鞋對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 :public class Test extends Activity private LinearLayout mBackgroundLayout;private TextViewTest mTextViewTest;/* Called when the activity is first created. */Overridepublic void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);mBackgroundLayout = new MyLayout(this);mBackgroundLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);mTextViewTest = new TextViewTest(this);mBackgroundLayout.addView(mTextViewTest);setContentView(mBackgroundLayout);public class MyLayout extends LinearLayoutpublic MyLayout(Context context) super(context);/ TODO Auto-generated constructor stubOverrideprotected void onLayout(boolean changed, int l, int t, int r, int b) / TODO Auto-generated method stubsuper.onLayout(changed, l, t, r, b);Log.i(Tag, -);View mView=getChildAt(0);mView.measure(0, 0);public class TextViewTest extends TextView public TextViewTest(Context context) super(context);/ TODO Auto-generated constructor stubsetText(test test );Override protected void onDraw(Canvas canvas) / TODO Auto-generated method stub super.onDraw(canvas); / measure(0, 0); Log.i(Tag, width: + getWidth() + ,height: + getHeight(); Log.i(Tag, MeasuredWidth: + getMeasuredWidth()+ ,MeasuredHeight: + getMeasuredHeight(); 這裡是在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的內容佔據的實際寬度。你如果想從一個最簡單的例子中的到它們的不同,下面將對上面的例子做一下修改:public class Test extends Activity private TextViewTest mTextViewTest;/* Called when the activity is first created. */Overridepublic void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);mTextViewTest = new TextViewTest(this);setContentView(mTextViewTest);public class TextViewTest extends TextView public TextViewTest(Context context) super(context);/ TODO Auto-generated constructor stubsetText(test test );Overrideprotected void onDraw(Canvas canvas) / TODO Auto-generated method stubsuper.onDraw(canvas);measure(0, 0);Log.i(Tag, width: + getWidth() + ,height: + getHeight();Log.i(Tag, MeasuredWidth: + getMeasuredWidth()+ ,MeasuredHeight: + getMeasuredHeight();總結: getWidth(): View在設定好佈局後整個View的寬度。 getMeasuredWidth(): 對View上的內容進行測量後得到的View內容佔據的寬度,前提是你必須在父佈局的onLayout()方法或者此View的onDraw()方法裡調用measure(0,0);(measure 參數的值你可

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论