转自:鸿洋博客,正在学习,非常感谢!
前面学习的自定义控件都是继承View现在学习继承ViewGroup
1、ViewGroup的职责是啥?
ViewGroup相当于一个放置View的容器,并且我们在写布局xml的时候,会告诉容器(凡是以layout为开头的属性,都是为用于告诉容器的),我们的宽度(layout_width)、高度(layout_height)、对齐方式(layout_gravity)等;当然还有margin等;于是乎,ViewGroup的职能为:给childView计算出建议的宽和高和测量模式 ;决定childView的位置;为什么只是建议的宽和高,而不是直接确定呢,别忘了childView宽和高可以设置为wrap_content,这样只有childView才能计算出自己的宽和高。
2、View的职责是啥?
View的职责,根据测量模式和ViewGroup给出的建议的宽和高,计算出自己的宽和高;同时还有个更重要的职责是:在ViewGroup为其指定的区域内绘制自己的形态。
3、ViewGroup和LayoutParams之间的关系?
大家可以回忆一下,当在LinearLayout中写childView的时候,可以写layout_gravity,layout_weight属性;在RelativeLayout中的childView有layout_centerInParent属性,却没有layout_gravity,layout_weight,这是为什么呢?这是因为每个ViewGroup需要指定一个LayoutParams,用于确定支持childView支持哪些属性,比如LinearLayout指定LinearLayout.LayoutParams等。如果大家去看LinearLayout的源码,会发现其内部定义了LinearLayout.LayoutParams,在此类中,你可以发现weight和gravity的身影。
2、View的3种测量模式
上面提到了ViewGroup会为childView指定测量模式,下面简单介绍下三种测量模式:
EXACTLY:表示设置了精确的值,一般当childView设置其宽、高为精确值、match_parent时,ViewGroup会将其设置为EXACTLY;
AT_MOST:表示子布局被限制在一个最大值内,一般当childView设置其宽、高为wrap_content时,ViewGroup会将其设置为AT_MOST;
UNSPECIFIED:表示子布局想要多大就多大,一般出现在AadapterView的item的heightMode中、ScrollView的childView的heightMode中;此种模式比较少见。
注:上面的每一行都有一个一般,意思上述不是绝对的,对于childView的mode的设置还会和ViewGroup的测量mode有一定的关系;当然了,这是第一篇自定义ViewGroup,而且绝大部分情况都是上面的规则,所以为了通俗易懂,暂不深入讨论其他内容。
3、从API角度进行浅析
上面叙述了ViewGroup和View的职责,下面从API角度进行浅析。
View的根据ViewGroup传人的测量值和模式,对自己宽高进行确定(onMeasure中完成),然后在onDraw中完成对自己的绘制。
ViewGroup需要给View传入view的测量值和模式(onMeasure中完成),而且对于此ViewGroup的父布局,自己也需要在onMeasure中完成对自己宽和高的确定。此外,需要在onLayout中完成对其childView的位置的指定。
4、完整的例子
需求:我们定义一个ViewGroup,内部可以传入0到4个childView,分别依次显示在左上角,右上角,左下角,右下角
1、决定该ViewGroup的LayoutParams
对于我们这个例子,我们只需要ViewGroup能够支持margin即可,那么我们直接使用系统的MarginLayoutParams;重写父类的该方法,返回MarginLayoutParams的实例,这样就为我们的ViewGroup指定了其LayoutParams为MarginLayoutParams。
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new MarginLayoutParams(getContext(),attrs);
}
2、onMeasure
在onMeasure中计算childView的测量值以及模式,以及设置自己的宽和高:
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//首先获取viewGroup的上级容器给其推荐的宽高和计算模式
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int HeightSize = MeasureSpec.getSize(heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
//计算出所有childview的宽和高
measureChildren(widthMeasureSpec, heightMeasureSpec);
//这里要判断当前的计算模式,是不是wrap_content
int width, height;//viewGroup的宽和高
//孩子的宽和高
int cWidth = 0;
int cHeight = 0;
int childCount = getChildCount();//所有孩子的大小,这里我们设置的是4
MarginLayoutParams cParams = null;
//高度和宽度分别是按照左右两个高度来设置
int rHeight = 0;//右边两个child的高度
int lHeight = 0;//左边两个view的高度和左边的高度相比取最大值
int tWidth = 0;//上面另个的宽
int bWidth = 0;//下面两个的宽,最终和上面的相比取最大值
for (int i = 0; i < childCount; i++) {//这里我们设置的大小是四
View childView = getChildAt(i);
cWidth = childView.getMeasuredWidth();
cHeight = childView.getMeasuredHeight();
cParams = (MarginLayoutParams) childView.getLayoutParams();
//上面两个view的宽
if (i == 0 || i == 1) {
tWidth += cWidth + cParams.leftMargin + cParams.rightMargin;
}
//下面两个view的宽
if (i == 2 || i == 3) {
bWidth += cWidth + cParams.leftMargin + cParams.rightMargin;
}
//左侧两个view的高
if (i == 0 || i == 2) {
lHeight += cHeight + cParams.topMargin + cParams.bottomMargin;
}
//右侧两个view的高
if (i == 1 || i == 3) {
rHeight += cHeight + cParams.topMargin + cParams.bottomMargin;
}
//这两个值是在wrap的时候使用
width = Math.max(tWidth, bWidth);
height = Math.max(lHeight, rHeight);
// setMeasuredDimension((widthMode == MeasureSpec.EXACTLY ? widthSize : width), (heightMode == MeasureSpec.EXACTLY ? HeightSize : height));
setMeasuredDimension((widthMode == MeasureSpec.EXACTLY) ? widthSize : Math.max(widthSize, width), (heightMode == MeasureSpec.EXACTLY) ? HeightSize : Math.max(HeightSize, height));//这个和鸿洋的是一个效果
Log.e("yqy","大小"+widthMode+","+heightMode+"----"+widthSize+"===="+width+"==="+height+"====="+HeightSize+"===="+MeasureSpec.EXACTLY);
}
首先,获取该ViewGroup父容器为其设置的计算模式和尺寸,大多情况下,只要不是wrap_content,父容器都能正确的计算其尺寸。所以我们自己需要计算如果设置为wrap_content时的宽和高,如何计算呢?那就是通过其childView的宽和高来进行计算。
然后,通过ViewGroup的measureChildren方法为其所有的孩子设置宽和高,此行执行完成后,childView的宽和高都已经正确的计算过了
同时,根据childView的宽和高,以及margin,计算ViewGroup在wrap_content时的宽和高。
最后,如果宽高属性值为wrap_content,则设置为43-71行中计算的值,否则为其父容器传入的宽和高。
3、onLayout对其所有childView进行定位(设置childView的绘制区域)
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childrenCount = getChildCount();
int cWidth = 0;
int cHeight = 0;
View view = null;
MarginLayoutParams cParams = null;
for (int i = 0; i < childrenCount; i++) {
view = getChildAt(i);
cParams = (MarginLayoutParams) view.getLayoutParams();
cWidth = view.getMeasuredWidth();
cHeight = view.getMeasuredHeight();
//左右四个角,这里是用margin布局,所以只需要计算四个view四个margin
int cl = 0, cr = 0, ct = 0, cb = 0;
switch (i) {
case 0:
cl = cParams.leftMargin;
ct = cParams.topMargin;
break;
case 1:
//cl = getWidth() -( cWidth + cParams.leftMargin+ cParams.rightMargin),viewgroup的宽度减去子view的宽度得到距离左边的距离。
ct = cParams.topMargin;
cl = getWidth() - (cParams.leftMargin + cWidth + cParams.rightMargin);
break;
case 2:
cl = cParams.leftMargin;
ct = getHeight() - (cParams.topMargin + cHeight);
break;
case 3:
cl = getWidth() - (cParams.leftMargin + cWidth + cParams.rightMargin);
ct = getHeight() - (cParams.topMargin + cHeight);
break;
}
cr = cWidth + cl;
cb = cHeight + ct;
view.layout(cl, ct, cr, cb);
}
}
代码比较容易懂:遍历所有的childView,根据childView的宽和高以及margin,然后分别将0,1,2,3位置的childView依次设置到左上、右上、左下、右下的位置。
如果是第一个View(index=0) :则childView.layout(cl, ct, cr, cb); cl为childView的leftMargin , ct 为topMargin , cr 为cl+ cWidth , cb为 ct + cHeight
如果是第二个View(index=1) :则childView.layout(cl, ct, cr, cb);
cl为getWidth() - cWidth - cParams.leftMargin- cParams.rightMargin;
ct 为topMargin , cr 为cl+ cWidth , cb为 ct + cHeight
剩下两个类似~
这样就完成了,我们的ViewGroup代码的编写,下面我们进行测试,分别设置宽高为固定值,wrap_content,match_parent
测试结果:
test1:
<com.example.down.customeviewdemo_5.CustomeView5
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="10dp"
android:background="#ddd">
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#f88"
android:gravity="center"
android:text="1" />
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#093"
android:gravity="center"
android:text="2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0ff"
android:gravity="center"
android:text="3" />
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#f00"
android:gravity="center"
android:text="4" />
</com.example.down.customeviewdemo_5.CustomeView5>
test2:
<com.example.down.customeviewdemo_5.CustomeView5
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="10dp"
android:background="#cd5">
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#f88"
android:gravity="center"
android:text="1" />
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#093"
android:gravity="center"
android:text="2" />
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#0ff"
android:gravity="center"
android:text="3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#f00"
android:gravity="center"
android:text="4"
android:textSize="20dp" />
</com.example.down.customeviewdemo_5.CustomeView5>
test3:
<com.example.down.customeviewdemo_5.CustomeView5
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="10dp"
android:background="#cd5">
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#f88"
android:gravity="center"
android:text="1" />
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#093"
android:gravity="center"
android:text="2" />
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#0ff"
android:gravity="center"
android:text="3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#f00"
android:gravity="center"
android:text="4"
android:textSize="20dp" />
</com.example.down.customeviewdemo_5.CustomeView5>
test4:
<com.example.down.customeviewdemo_5.CustomeView5
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#cdc">
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#f88"
android:gravity="center"
android:text="1" />
<TextView
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#093"
android:gravity="center"
android:text="2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0ff"
android:gravity="center"
android:text="3" />
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#f00"
android:gravity="center"
android:text="4" />
</com.example.down.customeviewdemo_5.CustomeView5>
test5:
<com.example.down.customeviewdemo_5.CustomeView5
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#ddd">
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#f88"
android:gravity="center"
android:text="1" />
<TextView
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#093"
android:gravity="center"
android:text="2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0ff"
android:gravity="center"
android:text="3" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#008"
android:gravity="center"
android:text="4" />
</com.example.down.customeviewdemo_5.CustomeView5>
test6:
<com.example.down.customeviewdemo_5.CustomeView5
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:background="#ddd">
<TextView
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#E5ED05"
android:gravity="center"
android:text="0"
android:textColor="#FFFFFF"
android:textSize="22sp"
android:textStyle="bold" />
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#00ff00"
android:gravity="center"
android:text="1"
android:textColor="#FFFFFF"
android:textSize="22sp"
android:textStyle="bold" />
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#ff0000"
android:gravity="center"
android:text="2"
android:textColor="#FFFFFF"
android:textSize="22sp"
android:textStyle="bold" />
<TextView
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#0000ff"
android:gravity="center"
android:text="3"
android:textColor="#FFFFFF"
android:textSize="22sp"
android:textStyle="bold" />
</com.example.down.customeviewdemo_5.CustomeView5>
test7:
<com.example.down.customeviewdemo_5.CustomeView5
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#000">
<TextView
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#E5ED05"
android:gravity="center"
android:text="0"
android:textColor="#FFFFFF"
android:textSize="22sp"
android:textStyle="bold" />
<TextView
android:layout_width="120dp"
android:layout_height="120dp"
android:background="#00ff00"
android:gravity="center"
android:text="1"
android:textColor="#FFFFFF"
android:textSize="22sp"
android:textStyle="bold" />
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#ff0000"
android:gravity="center"
android:text="2"
android:textColor="#FFFFFF"
android:textSize="22sp"
android:textStyle="bold" />
<TextView
android:layout_width="150dp"
android:layout_height="120dp"
android:background="#f88"
android:gravity="center"
android:text="3"
android:textColor="#FFFFFF"
android:textSize="22sp"
android:textStyle="bold" />
</com.example.down.customeviewdemo_5.CustomeView5>
test8:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#f00">
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#E5ED05"
android:gravity="center"
android:text="0"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:textSize="22sp"
android:textStyle="bold" />
<TextView
android:layout_width="70dp"
android:layout_height="70dp"
android:background="#00ff00"
android:gravity="center"
android:text="1"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:textSize="22sp"
android:textStyle="bold" />
<TextView
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#ff0000"
android:gravity="center"
android:text="2"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:textSize="22sp"
android:textStyle="bold" />
<TextView
android:layout_width="100dp"
android:layout_height="70dp"
android:background="#f88"
android:gravity="center"
android:text="3"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:textSize="22sp"
android:textStyle="bold" />
</RelativeLayout>
test9:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#093">
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#E5ED05"
android:gravity="center"
android:text="0"
android:layout_marginTop="0dp"
android:layout_marginLeft="0dp"
android:textSize="22sp"
android:textStyle="bold" />
<TextView
android:layout_width="70dp"
android:layout_height="70dp"
android:background="#00ff00"
android:gravity="center"
android:text="1"
android:layout_marginTop="0dp"
android:layout_marginLeft="130dp"
android:textSize="22sp"
android:textStyle="bold" />
<TextView
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#ff0000"
android:gravity="center"
android:text="2"
android:layout_marginTop="150dp"
android:layout_marginLeft="0dp"
android:textSize="22sp"
android:textStyle="bold" />
<TextView
android:layout_width="100dp"
android:layout_height="70dp"
android:background="#f88"
android:gravity="center"
android:text="3"
android:textSize="22sp"
android:layout_marginLeft="200dp"
android:layout_marginTop="150dp"
android:textStyle="bold" />
</RelativeLayout>
笔记:
//我对于view和ViewGroup的理解就是view是基本的控件,而viewGroup是存放控件的容器,在onlayout方法中指定view的位置
1.你的这个最后一个如果ViewGroup宽高都设置成wrap 左上角跟右下角会重合覆盖。
的确是有这个问题,然后我就用relativeLayout布局,结果是这样的:
demo上的:
relativeLayout布局上的,这个用的是layout_alignParent:
relativeLayout布局上的,这个用的是margin:
2.setMeasuredDimension((widthMode == MeasureSpec.EXACTLY) ? sizeWidth: Math.min(sizewidth,width), (heightMode == MeasureSpec.EXACTLY)? sizeHeight : Math.min(sizeHeight,height));
试了试,我设置的九个布局中会有四个不显示
log输出:
Log.e("yqy","大小"+widthMode+","+heightMode+"----"+widthSize+"===="+width+"==="+height+"====="+HeightSize+"===="+MeasureSpec.EXACTLY);
06-30 23:11:38.657 2799-2799/? E/yqy: 大小-2147483648,0----480====75===75=====0====1073741824
06-30 23:11:38.657 2799-2799/? E/yqy: 大小-2147483648,0----480====300===225=====0====1073741824
06-30 23:11:38.657 2799-2799/? E/yqy: 大小-2147483648,0----480====300===225=====0====1073741824
06-30 23:11:38.657 2799-2799/? E/yqy: 大小-2147483648,0----480====300===300=====0====1073741824
06-30 23:11:38.749 2799-2799/? E/yqy: 大小-2147483648,0----480====75===75=====0====1073741824
06-30 23:11:38.749 2799-2799/? E/yqy: 大小-2147483648,0----480====300===225=====0====1073741824
06-30 23:11:38.749 2799-2799/? E/yqy: 大小-2147483648,0----480====300===225=====0====1073741824
06-30 23:11:38.749 2799-2799/? E/yqy: 大小-2147483648,0----480====300===300=====0====1073741824
这里的height设置为0,然后我设置为求取最大值,最终和demo的效果是一样的