android getstring中getWidth和getMeasuredWidth之间的区别

getWidth()获得的宽度是View在设定好布局后整个View的宽度
getMeasuredWidth()是对View上的内容进行测量后得到的View内容占据的宽度。
getWidth得到是某个view的实际尺寸.&getMeasuredWidth是得到某view想要在parent view里面占的大小.&
两者的使用场合:
getMeasuredWidth:在自定义view重写onLayout时、在我们用layoutinflater动态加载view后想获得view的原始宽度时。
getWidth:一般在view已经布局后呈现出来了,想获取宽度时
阅读(...) 评论()android getMeausreWidth和getWidth的差别(一看就懂篇) &
android getMeausreWidth和getWidth的差别(一看就懂篇)
android getMeausreWidth和getWidth的差别(一看就懂篇)
最近有人问我android中创建自定义控件widget的时候,getWidth和getMeasureWidth的差别到底在哪里,网络上的文字总是说的模棱两可,copy来去~ 印象中getMeasureWidth是获得一个测量的宽度,getWidth获取的是View实际的宽度。
好,看到这里,您肯定想骂人,你这个说法和别人说的有什么不一样呀,别急,是的,这个就是网络上大多数文章的描述。【下面解释下测量的宽度,的获取是在调用measure()函数之后获取才是有意义的,否则您会发现获取出来的值和getWidth()效果是一样的。】
下面看段代码,这两个值的差异:
1.自定义控件布局如下:
&?xml version="1.0" encoding="utf-8"?&
&RelativeLayout xmlns:android="/apk/res/android"
android:id="@+id/customized"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/gray"&
android:id="@+id/textview1"
android:layout_width="200dip"
android:layout_height="20dip"
android:text="哈罗,我是Textview1"
android:layout_centerInParent="true"
android:background="@color/blue"&
&/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=-, heightMeasureSpec-
onMeasure - Width=0, Height=0, MeasuredWidth=432, MeasuredHeight=60
onMeasure - widthMeasureSpec=, heightMeasureSpec-
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
(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="/apk/res/android"
android:id="@+id/customized"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gray"&
android:id="@+id/textview1"
android:layout_width="200dip"
android:layout_height="20dip"
android:text="哈罗,我是Textview1"
android:layout_centerInParent="true"
android:background="@color/blue"&
&/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=-, heightMeasureSpec-
onMeasure - Width=0, Height=0, MeasuredWidth=432, MeasuredHeight=702
onMeasure - widthMeasureSpec=, heightMeasureSpec-
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
(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
Linux系统与内核学习群:
WP建站技术学习交流群:
魔豆的Linux内核之路
优秀工程师当看优秀书籍
赞助商广告View 的两对大小 getWidth getHeight getMeasuredWidth getMeasuredHeight
我的图书馆
View 的两对大小 getWidth getHeight getMeasuredWidth getMeasuredHeight
对于View的两个大小的API,以前有很多迷惑,不知道具体其含义,今天在看google官方文档的时候看到如下描述,终于恍然大悟。现记录如下:The size of a view is expressed with a width and a height. A view actually possess two pairs of width and height values.The first pair is known as&measured width&and&measured height. These dimensions define how big a view wants to be within its parent. The measured dimensions can be obtained by calling&&and.The second pair is simply known as&width&and&height, or sometimes&drawing width&and&drawing height. These dimensions define the actual size of the view on screen, at drawing time and after layout. These values may, but do not have to, be different from the measured width and height. The width and height can be obtained by calling&&and&.大概意思就是getWidth和getHeight获取的是View当前实际显示的大小,而getMeasured相关的函数则获取的是View在父类中计算的需要占用的大小。
TA的最新馆藏[转]&
喜欢该文的人也喜欢}

我要回帖

更多关于 android getfilesdir 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信