HardBirch

android getMeausreWidth和getWidth的差别(一看就懂篇)

时间:14-04-14 栏目:Android探究 作者:魔豆先生 评论:0 点击: 5,913 次

最近有人问我android中创建自定义控件widget的时候,getWidth和getMeasureWidth的差别到底在哪里,网络上的文字总是说的模棱两可,copy来去~ 印象中getMeasureWidth是获得一个测量的宽度,getWidth获取的是View实际的宽度

好,看到这里,您肯定想骂人,你这个说法和别人说的有什么不一样呀,别急,是的,这个就是网络上大多数文章的描述。【下面解释下测量的宽度,的获取是在调用measure()函数之后获取才是有意义的,否则您会发现获取出来的值和getWidth()效果是一样的。】

下面看段代码,这两个值的差异:

1.自定义控件布局如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/customized"
android:layout_width="wrap_content"
    android:layout_height="wrap_content"
android:background="@color/gray">

<TextView
android:id="@+id/textview1"
android:layout_width="200dip"
android:layout_height="20dip"
android:text="哈罗,我是Textview1"
android:layout_centerInParent="true"
android:background="@color/blue">
</TextView>
<TextView
android:id="@+id/textview2"
  android:layout_width="300dip"
android:layout_height="20dip"
android:text="哈罗,我是Textview2"
android:layout_below="@+id/textview1"
android:layout_centerHorizontal="true"
android:background="@color/blue">
</TextView>

</RelativeLayout>

2.运行输出LOG如下:

CustomizedView::Width=0, Height=0, MeasuredWidth=0, MeasuredHeight=0
onMeasure - widthMeasureSpec=-2147483216, heightMeasureSpec-2147482946
onMeasure - Width=0, Height=0, MeasuredWidth=432, MeasuredHeight=60
onMeasure - widthMeasureSpec=1073742256, heightMeasureSpec-2147482994
onMeasure - Width=0, Height=0, MeasuredWidth=432, MeasuredHeight=60
onLayout::l=24, t=24, r=456, b=84
onMeasure - widthMeasureSpec=0, heightMeasureSpec0
onMeasure - Width=432, Height=60, MeasuredWidth=450, MeasuredHeight=60
onLayout::Width=432, Height=60, MeasuredWidth=450, MeasuredHeight=60

 

3. 分析

(1) 构造函数里面宽度不能获取,尚未有效,都是0

(2)onLayout函数调用之前,getWidth()获取无效

(3)getMeasureWidth()可以获取到 控件内部元素布局后的width值,MeasuredWidth=450,是Textview2 的宽度值300dip的像素值

(4)由于MainActivity用的是工程默认的文件,两边各padding了16dip,所以剩余只有432像素点,getWidth()获得的是432,而不是450

 

///////////////////// 修改后布局与对应运行结果//////////////////////////////

1.自定义控件布局如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/customized"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gray">

<TextView
android:id="@+id/textview1"
android:layout_width="200dip"
android:layout_height="20dip"
android:text="哈罗,我是Textview1"
android:layout_centerInParent="true"
android:background="@color/blue">
</TextView>
<TextView
android:id="@+id/textview2"
android:layout_width="200dip"
android:layout_height="20dip"
android:text="哈罗,我是Textview2"
android:layout_below="@+id/textview1"
android:layout_centerHorizontal="true"
android:background="@color/blue">
</TextView>

</RelativeLayout>

 

2.运行输出LOG如下:

CustomizedView::Width=0, Height=0, MeasuredWidth=0, MeasuredHeight=0
onMeasure - widthMeasureSpec=-2147483216, heightMeasureSpec-2147482946
onMeasure - Width=0, Height=0, MeasuredWidth=432, MeasuredHeight=702
onMeasure - widthMeasureSpec=1073742256, heightMeasureSpec-2147482994
onMeasure - Width=0, Height=0, MeasuredWidth=432, MeasuredHeight=654
 onLayout::l=24, t=24, r=456, b=678
onMeasure - widthMeasureSpec=0, heightMeasureSpec0
 onMeasure - Width=432, Height=654, MeasuredWidth=300, MeasuredHeight=60
 onLayout::Width=432, Height=654, MeasuredWidth=300, MeasuredHeight=60

3.分析

(1)这里的控件测量的宽度是300,小于MainActivity的432,所以getMeasuredWidth()和getWidth()值一样

(2) 控件的外部 使用match_paren的时候getMeasureWidth不会计算宽度的值match部分的值,或者说getMeasureWidth()计算的是WrapContent的可以计算出来的那部分的宽度。

    android:layout_width="match_parent"
android:layout_height="match_parent"

(3)getHeight和getMeasureHeight同理。

(4)回头我们再看下

getWidth():

Return the width of the your view.

Returns: the width of your view, in pixels

getMeasuredWidth():

The width of this view as measured in the most recent call to measure(). This should be used during measurement and layout calculations only. Use getWidth() to see how wide a view is after layout.

Returns: the measured width of this view

 

声明: 本文由( 魔豆先生 )原创编译,转载请保留链接: android getMeausreWidth和getWidth的差别(一看就懂篇)

android getMeausreWidth和getWidth的差别(一看就懂篇):等您坐沙发呢!

发表评论


QQ群互动

Linux系统与内核学习群:194051772

WP建站技术学习交流群:194062106

魔豆之路QR

魔豆的Linux内核之路

魔豆的Linux内核之路

优秀工程师当看优秀书籍

优秀程序员,要看优秀书!

赞助商广告

友荐云推荐