android自定义View之TabLayout(继承自ViewGroup)

        目前自己的状态还是不是太好,太容易受到别人对人对自己的影响,如何做到只关注自己学习的状态,这确实是一个比较严肃点问题。

        本次的学习总结也是比较简单的自定义ViewGroup,主要是实现标签(Tag)在这个ViewGroup中能够正确的测量和摆放。对应的两个重要的方法:onMeasure()、onLayout()。

        特别注意:List<List <View>>的赋值过程。在测量ViewGroup子孩子的过程中,需要提前调用

 measureChild(childView,widthMeasureSpec,heightMeasureSpec);

上面这个方法需要传入父布局的

,widthMeasureSpec,heightMeasureSpec


才能通过

int childWidth = childView.getMeasuredWidth();
的方式得到子孩子的宽高。

    

public class TagViewGoup extends ViewGroup {

    int width = 0,height = 0;
    int childCount;
    private static final String TAG = "TagViewGoup";
    List<List<View>> outList = new ArrayList<>();

    public TagViewGoup(Context context) {
        super(context);
    }

    public TagViewGoup(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TagViewGoup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        outList.clear();

        List<View> innerList = new ArrayList<>();
        outList.add(innerList);
        int width = 0;
        int Maxwidth = 0;
        int layoutWith = MeasureSpec.getSize(widthMeasureSpec);
        int childCount = getChildCount();
        for(int i = 0;i<childCount;i++){
            View childView = getChildAt(i);
            measureChild(childView,widthMeasureSpec,heightMeasureSpec);
            int childWidth = childView.getMeasuredWidth();
            width +=childWidth;
            if(width >layoutWith){
                Log.d(TAG, "onMeasure: ");
                int currentMax = width - childView.getMeasuredWidth();
                Maxwidth = Math.max(Maxwidth,currentMax);
                width = 0;
                height += childView.getMeasuredHeight();
                innerList = new ArrayList<>();
                outList.add(innerList);
            }else {
                height = childView.getMeasuredHeight();
            }
            innerList.add(childView);
            setMeasuredDimension(Maxwidth,height);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        int orginBottom = outList.get(0).get(0).getMeasuredHeight();
        int left = 0,top = 0,bottom = orginBottom,right = 0;
        for (List<View> viewList:outList){
            for(View childView:viewList){
                right += childView.getMeasuredWidth();
                childView.layout(left,top,right,bottom);
                left += childView.getMeasuredWidth();
            }
            bottom += orginBottom;
            top += orginBottom;
            left =0;
        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值