基于LinearLayout的小标签(TextView)自动换行(修改)

        设计最初是因为公司项目需要多处显示多个小标签,并且需要多行展示,最开始使用的GridLayout,但是这个网格布局局限性太高,标签是动态的,内容也不定,用GridLayout就会有多行占用的各种显示问题,所以后来换成了LinearLayout动态添加,写完以后就有了写一个继承LinearLayout的自动换行布局的想法。

        GridLayout的显示效果:

        即使根据长度自动设置占据行数,还是会有显示上的问题,更何况如果碰到符号类,实际长度又比判断内容长度时设想的短不少,显示出来就更丑了。

        优化之后的显示效果:

         优化之后,动态添加子View的时候会读子View的长度,判断长度大于一行就自动换行,基本达到了想要的效果,但是代码比较繁琐,多处使用的时候会比较麻烦,所以就写成了一个View,这样调用后传数据进去就可以自动生成了。

        (修改)修改原因是觉得自己写的函数太多,没办法融入到View的体系里面,刷新、内边距、外边距等操作都需要手动操作,用起来极其繁琐,所以修改了原来的实现方式,重载父类的onMeasure和onLayout来实现父View的动态高度和子View的排版,实现结果和原先的差不多,修改后由于子View在外面用addView自己实现,所以不再局限于TextView。

        接下来上代码:

        AutoItemLayout:

package com.mine.mylibrary.viewutil;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

public class AutoItemLayout extends LinearLayout {

    public AutoItemLayout(Context context) {
        this(context, null);
    }

    public AutoItemLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    private int getViewWidth(View view) {
        int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        view.measure(spec, spec);
        return view.getMeasuredWidth();
    }

    private int getViewHeight(View view) {
        int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        view.measure(spec, spec);
        return view.getMeasuredHeight();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        final int count = getChildCount();
        int measureHeight = 0;
        int lastRight = 0;
        int tempHeight = 0;
        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                final LayoutParams layoutParams = (LayoutParams) child.getLayoutParams();
                int childLong = lastRight + layoutParams.getMarginStart() + getViewWidth(child) + layoutParams.getMarginEnd();
                if (childLong > getMeasuredWidth()) {
                    measureHeight = measureHeight + tempHeight;
                    tempHeight = 0;
                    lastRight = layoutParams.getMarginStart() + getViewWidth(child) + layoutParams.getMarginEnd();
                    int compareHeight = layoutParams.topMargin + getViewHeight(child) + layoutParams.bottomMargin;
                    tempHeight = Math.max(compareHeight, tempHeight);
                } else {
                    int compareHeight = layoutParams.topMargin + getViewHeight(child) + layoutParams.bottomMargin;
                    tempHeight = Math.max(compareHeight, tempHeight);
                    lastRight = lastRight + layoutParams.getMarginStart() + getViewWidth(child) + layoutParams.getMarginEnd();
                }
            }
        }
        if (tempHeight != 0) {
            measureHeight += tempHeight;
        }
        setMeasuredDimension(getMeasuredWidth(), measureHeight);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int count = getChildCount();
        int lastRight = 0;
        int lastBottom = 0;
        int left, top, right, bottom;
        int tempHeight = 0;
        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                final LayoutParams layoutParams = (LayoutParams) child.getLayoutParams();
                int childLong = lastRight + layoutParams.getMarginStart() + getViewWidth(child) + layoutParams.getMarginEnd();
                if (childLong > getMeasuredWidth()) {
                    lastBottom = lastBottom + tempHeight;
                    tempHeight = 0;
                    left = layoutParams.getMarginStart();
                    top = lastBottom + layoutParams.topMargin;
                    right = layoutParams.getMarginStart() + getViewWidth(child);
                    bottom = lastBottom + layoutParams.topMargin + getViewHeight(child);
                    lastRight = layoutParams.getMarginStart() + getViewWidth(child) + layoutParams.getMarginEnd();

                    int compareHeight = layoutParams.topMargin + getViewHeight(child) + layoutParams.bottomMargin;
                    tempHeight = Math.max(compareHeight, tempHeight);
                } else {
                    left = lastRight + layoutParams.getMarginStart();
                    top = lastBottom + layoutParams.topMargin;
                    right = lastRight + layoutParams.getMarginStart() + getViewWidth(child);
                    bottom = lastBottom + layoutParams.topMargin + getViewHeight(child);
                    lastRight = lastRight + layoutParams.getMarginStart() + getViewWidth(child) + layoutParams.getMarginEnd();

                    int compareHeight = layoutParams.topMargin + getViewHeight(child) + layoutParams.bottomMargin;
                    tempHeight = Math.max(compareHeight, tempHeight);
                }
                child.layout(left, top, right, bottom);
            }
        }
    }

}

        XML代码:

<com.example.viewutil.AutoLinearLayout
        android:id="@+id/auto_linearlayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"/>

        Activity中的代码:

    AutoItemLayout autoLinearLayout = findViewById(R.id.auto_linearlayout);
    StringBuilder showString = new StringBuilder();
    List<AutoViewDataModel> viewList = new ArrayList<>();
    for (int i = 0; i < 40; i++) {
        if (i % 11 == 0) {
            showString = new StringBuilder();
        }
        showString.append("啦");
        viewList.add(new AutoViewDataModel(String.valueOf(i), showString.toString()));
    }
    for (AutoViewDataModel dataModel : viewList) {
        TextView textView = new TextView(this);
        LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        textParams.setMargins(DensityUtil.dip2px(this, 10), DensityUtil.dip2px(this, 5),
                DensityUtil.dip2px(this, 10), DensityUtil.dip2px(this, 5));
        textView.setLayoutParams(textParams);
        textView.setPadding(DensityUtil.dip2px(this, 7), DensityUtil.dip2px(this, 4),
                DensityUtil.dip2px(this, 7), DensityUtil.dip2px(this, 4));
        textView.setBackground(DrawCorner.drawCorner(15, getColor(R.color.white)));
        textView.setTextSize(14);
        textView.setTextColor(getColor(R.color.blue));
        textView.setTag(dataModel.getId());
        textView.setText(dataModel.getName());
        autoLinearLayout.addView(textView);
    }

        这里使用的DrawCorner在我的另一篇博客中有讲,是一个设置圆角背景色边框的工具类。

        最后效果图:

 结束,感谢阅读!

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值