自定义taglayout,记二撸自定义view

自定义标签布局

效果如下:
在这里插入图片描述

package com.iyuba.pratice.view;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by maoyujiao on 2019/11/18.
 */

public class TagLayout extends ViewGroup {
    private List<Rect> bounds = new ArrayList<>();
    private MarginLayoutParams marginLayoutParams;

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

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //自定义layout不需要调用suer.onMeasure(),但别忘记最后调用setMeasuredDimension(),来保存该layout的大小
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //fix 第一次使用的全局变量useWith,useHeight,第一个子view但发现会显示有问题,不是左上角而是显示在中间的一个位置
        //注意,onMeasure方式会每16ms调用一次,所以如果用全局的话,初始化第一行的useWith,useHeight就不是0,就会有问题。
        //注意按此全局逻辑则useheight应该会一直增加的,但实际后面刷新几次后该view位置就不在变化了,猜测可能后面就不会调用RequestLayout重新测量布局了,后面在验证吧。
        int useWith = 0;
        int useHeight = 0;
        int maxHeight = 0;
        int with = MeasureSpec.getSize(widthMeasureSpec);
        for (int i = 0; i < getChildCount(); i++) {
            View view = getChildAt(i);
            marginLayoutParams = (MarginLayoutParams) view.getLayoutParams();
            if (useWith == 0) {
                useHeight += marginLayoutParams.topMargin;
            }
            useWith += marginLayoutParams.leftMargin;
            //内部会自己算child withMeasureSpeci,heghtMeasureSpci
            //当withUsed >= with时,不会在测量了,即下一个子view的measureWith为0,故此处 withUsed为0。
            measureChild(view, widthMeasureSpec, heightMeasureSpec);
            if (useWith + view.getMeasuredWidth() + marginLayoutParams.rightMargin > with) {
                useWith = 0;
                useHeight += maxHeight + marginLayoutParams.bottomMargin; //注意使用getMeasureHeight,使用getHeight会是0
                maxHeight = 0;
            }
            maxHeight = Math.max(maxHeight, view.getMeasuredHeight());
            //记录下子view的位置,以便在onlayout方式,调用子view的layout(left,top,right,bottom)
            //注意此处list的添加方式
            Rect childBound;
            if (bounds.size() <= i) {
                childBound = new Rect();
                bounds.add(childBound);
            } else {
                childBound = bounds.get(i);
            }
            childBound.set(new Rect(useWith != 0 ? useWith : useWith + marginLayoutParams.leftMargin,
                    useHeight,
                    useWith + view.getMeasuredWidth(),
                    useHeight + view.getMeasuredHeight()));
            useWith += view.getMeasuredWidth() + marginLayoutParams.rightMargin;
        }
        setMeasuredDimension(with, useHeight);

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        for (int i = 0; i < getChildCount(); i++) {
            View view = getChildAt(i);
            view.layout(bounds.get(i).left,
                    bounds.get(i).top,
                    bounds.get(i).right,
                    bounds.get(i).bottom);
        }

    }

    /**
     * 当view.getLayoutParam,强转为MarginLayoutParam时,需要重写该方法
     * @param attrs
     * @return
     */
    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }
}

xml使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.iyuba.pratice.MainActivity2">

    <com.iyuba.pratice.view.TagLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:padding="10dp"
            android:background="@drawable/bg_taglayout"
            android:lines="1"
            android:text="我爱你中国,one"
            android:textSize="14sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="@drawable/bg_taglayout"
            android:lines="1"
            android:text="我爱你中国,twotwo"
            android:textSize="14sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="@drawable/bg_taglayout"
            android:lines="1"
            android:text="我爱你中国,twotwotwotwo"
            android:textSize="14sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="@drawable/bg_taglayout"
            android:lines="1"
            android:text="我爱你中国,twotwotwotwo"
            android:textSize="14sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="@drawable/bg_taglayout"
            android:lines="1"
            android:text="我爱你中国"
            android:textSize="14sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="@drawable/bg_taglayout"
            android:lines="1"
            android:text="我爱你中国"
            android:textSize="14sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="@drawable/bg_taglayout"
            android:lines="1"
            android:text="我爱你中国,twotwotwotwotwotwotwotwo"
            android:textSize="14sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="@drawable/bg_taglayout"
            android:lines="1"
            android:text="我爱你中国"
            android:textSize="14sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="@drawable/bg_taglayout"
            android:lines="1"
            android:text="我爱你中国"
            android:textSize="14sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/bg_taglayout"
            android:lines="1"
            android:layout_margin="5dp"
            android:text="我爱你中国"
            android:textSize="14sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="@drawable/bg_taglayout"
            android:text="我爱你中国"
            android:textSize="14sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/bg_taglayout"
            android:text="我爱你中国"
            android:layout_margin="5dp"
            android:textSize="14sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/bg_taglayout"
            android:text="我爱你中国"
            android:layout_margin="5dp"
            android:textSize="14sp" />


    </com.iyuba.pratice.view.TagLayout>
</LinearLayout>

总结

自定义viewGroup,
1.重写onMeasure方法,测量每个子view的大小
2.保存子view的位置,通过List
3.设置该Layout的大小
4.重写onLayout方法,调用每个子view的layout方法,并传入子view 的位置

注意:
1.测量每个子view 的大小,实际就是调用子view的measure方法,计算并传入WithmeasureSpec,HeightmeasureSpec。该过程ViewGroup已经封装,可以直接调用MeasureChild方法
2.一般情况下,自定义view直接使用局部变量就行,以防每16ms调用一次,使用全局变量会出问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值