继承FlexboxLayout自定义可自动换行的tag标签

实现效果如图:

 

通过继承自FlexboxLayout实现,Java代码:

package org.test;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayout;
import com.google.android.flexbox.JustifyContent;

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

public class TagGroup extends FlexboxLayout {
    private List<String> mTexts;
    private List<Integer> mColors;
    private Context mContext;
    private int TAG_VIEW_COUNT = 0;

    public TagGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    private void init() {
        mTexts = new ArrayList<>();
        mColors = new ArrayList<>();

        this.setFlexDirection(FlexDirection.ROW);
        this.setJustifyContent(JustifyContent.FLEX_START);
        this.setFlexWrap(FlexWrap.WRAP);
    }

    public void setTagView(@NonNull List<String> texts, @Nullable List<Integer> colors) {
        if (texts == null || texts.size() == 0) {
            throw new RuntimeException("tag view文本字段不能为空");
        }

        this.mTexts = texts;
        TAG_VIEW_COUNT = texts.size();

        if (colors == null || colors.size() == 0) {
            for (int i = 0; i < TAG_VIEW_COUNT; i++) {
                mColors.clear();
                mColors.add(Color.WHITE);
            }
        } else {
            this.mColors = colors;
        }

        this.removeAllViews();

        for (int i = 0; i < TAG_VIEW_COUNT; i++) {
            TextView textView = makeTextView(mTexts.get(i), mColors.get(i));
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            //设置每一个子View在整体布局中与其他子View的上下左右的margin
            layoutParams.setMargins(0, 1, 5, 1);

            this.addView(textView, layoutParams);
        }

        this.invalidate();
    }

    //绘制圆角描边的TextView
    private TextView makeTextView(String s, Integer c) {
        TextView textView = new TextView(mContext);
        textView.setText(s);
        textView.setPadding(10, 5, 10, 5);

        int strokeWidth = 5; // 5px
        int roundRadius = 15; // 15px
        int strokeColor = Color.GRAY;
        int fillColor = c;

        GradientDrawable gd = new GradientDrawable();
        gd.setColor(fillColor);
        gd.setCornerRadius(roundRadius);
        gd.setStroke(strokeWidth, strokeColor);

        textView.setBackground(gd);

        return textView;
    }
}

 

使用方式,把TagGroup当作一个普通布局使用,写入xml布局:

    <org.test.TagGroup
        android:id="@+id/tag_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

 

然后在上层,通过set方式,添加子tag标签实现:

        TagGroup tagGroup = findViewById(R.id.tag_group);
        List<String> texts = Arrays.asList("zhang", "phil", "csdn", "android", "zhang", "phil", "csdn", "android", "zhang", "phil", "csdn", "android");
        List<Integer> colors = Arrays.asList(Color.RED, Color.DKGRAY, Color.BLUE, Color.RED, Color.DKGRAY, Color.BLUE, Color.RED, Color.DKGRAY, Color.BLUE, Color.RED, Color.DKGRAY, Color.BLUE);
        tagGroup.setTagView(texts, colors);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangphil

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值