作用:根据父容器传递跟子容器的大小要求来确定子容器的大小。
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)的参数说明和MeasureSpec类的说明:
通过MeasureSpec这个类可以获取父View传递过来的一些信息,包括MODE、SIZE属性。这里做一下说明
当MODE为AT_MOST时,SIZE大小为父容器所能提供的最大值。
当MODE为EXACTLY时,SIZE为父容器提供的限制值。
当MODE为UNSPECIFIED时,大小为0,SIZE完全由子容器的大小决定。
AT_MOST:子容器可以是声明大小内的任意大小
EXACTLY:父容器已经为子容器确定的大小,子容器应该遵守
UNSPECIFIED:父容器对子容器没有做任何限制,子容器可以任意大小
MODE:分为一下三种类别,
SIZE是父容器为子容器提供的大小
实例说明:
(1)首先我们新建一个View,重写onMeasure()方法.
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(measureHeight(widthMeasureSpec), measureHeight(heightMeasureSpec));
}
private int measureHeight(int heightMeasureSpec) {
// TODO Auto-generated method stub
int result=0; //结果
int specMode=MeasureSpec.getMode(heightMeasureSpec);
int specSize=MeasureSpec.getSize(heightMeasureSpec);
switch (specMode) {
case MeasureSpec.AT_MOST: // 子容器可以是声明大小内的任意大小
Log.e(Tag, "子容器可以是声明大小内的任意大小");
Log.e(Tag, "大小为:"+specSize);
result=specSize;
break;
case MeasureSpec.EXACTLY: //父容器已经为子容器设置了尺寸,子容器应当服从这些边界,不论子容器想要多大的空间. 比如EditTextView中的DrawLeft
Log.e(Tag, "父容器已经为子容器设置了尺寸,子容器应当服从这些边界,不论子容器想要多大的空间");
Log.e(Tag, "大小为:"+specSize);
result=specSize;
break;
case MeasureSpec.UNSPECIFIED: //父容器对于子容器没有任何限制,子容器想要多大就多大. 所以完全取决于子view的大小
Log.e(Tag, "父容器对于子容器没有任何限制,子容器想要多大就多大");
Log.e(Tag, "大小为:"+specSize);
result=1500;
break;
default:
break;
}
return result;
}
(2)然后我们在布局文件中写如下布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<com.example.circleimage.CustomView1
android:layout_width="1999dp"
android:layout_height="40dp"
android:background="#99CCCC" />
</HorizontalScrollView>
<com.example.circleimage.CustomView1
android:layout_width="wrap_content"
android:layout_height="40dp"
android:background="#99CCCC" />
</LinearLayout>
![](http://pubimage.360doc.com/wz/default.gif)
这个是运行后的结果,从上面我们可以看出:
第一个CustomView1的父容器为HorizontalScrollView,HorizontalScrollView对它的子容器的width是没有任何限制的,所以当width设置为19999dp时子容器的宽度去决定于自己的宽度(属于UNSPECIFIED),size大小为0.
第一个CustomView1的高度设置为40dp时(80px),这时候父容器为它设定了具体的值(属于EXACTLY)
第二个CustomView1的宽度设置为wrap_content时,这时父容器为它做了限制,只要在父容器设置的尺寸(720px)之内都可以。