Android getWidth和getMeasuredWidth(转)
一。也很多童鞋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. */ @Override public 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 LinearLayout{ public MyLayout(Context context) { super(context); // TODO Auto-generated constructor stub } @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("Tag", "--------------"); View mView=getChildAt(0); mView.measure(0, 0); } } public class TextViewTest extends TextView { public TextViewTest(Context context) { super(context); // TODO Auto-generated constructor stub setText("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. */ @Override public 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 stub setText("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()); } }}?
(正解):
? getWidth(): View在定好局後整View的度。
? getMeasuredWidth(): View上的容行量後得到的View容的度,前提是你必在父局的onLayout()方法或者此View的onDraw()方法用measure(0,0);(measure 的值你可以自己定),否你得到的果和getWidth()得到的果一。
?????? 也我的不是很好,大家有什不清楚的地方再我留言。於方法的就是看你有有用measure()方法,然measure()的位置也是很重要的。
三.尊重原,明是http://hi.baidu.com/ljlkings/home的空。