自定义ViewGroup

自定义Viewgroup

1.创建一个JAVA类继承于viewGroup

2.重写构造器

3.复写onLayout方法

4.在onMeasure方法下让子View测量自己

measureChildren(width,height);//让在这个View下的子View自己测量自己(第一个参数是这个View的所要求的宽,第二个参数是这个View所要求的长度)

5.最后得到子View,然后对其布局进行处理

  @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        View child1=getChildAt(0);
        View child2=getChildAt(1);
        View child3=getChildAt(2);
        View child4=getChildAt(3);
        if (child1!=null){
            child1.layout(0,0,child1.getMeasuredWidth(),child1.getMeasuredHeight());
        }
        if (child2!=null){
            child2.layout(r-child2.getMeasuredWidth(),0,r,child2.getMeasuredHeight());
        }
        if (child3!=null){
            child3.layout(0,b-child3.getMeasuredHeight(),child3.getMeasuredWidth(),b);
        }
        if (child4!=null){
            child4.layout(r - child4.getMeasuredWidth(), b - child4.getMeasuredHeight(),r,b);
        }
    }

处理的结果是

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值