自定义使其子Textview可以根据宽度自动换行的LinearLayout

package com.lahm.learndaemon.view;

/**
 * ================================================
 * Description:自定义使其子Textview可以根据宽度自动换行的LinearLayout
 * <p>
 * Created by mz on 2023/9/11 13:57
 * ================================================
 */

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
    public 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
    public 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);
            }
        }
    }
}

该LinearLayout作为父类,再其布局文件中添加子类,可以根据宽度进行自动换行

    <com.lahm.learndaemon.view.AutoItemLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:textSize="@dimen/x24"
            android:textColor="@color/color_text_333"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:filterTouchesWhenObscured="false"
            android:text="查看并同意"
            />
        <TextView

            android:textColor="@color/color_text_333"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:filterTouchesWhenObscured="false"
            android:textSize="@dimen/x24"
            android:text="《服务协议》"
            />
        <TextView
            android:textSize="@dimen/x24"
            android:textColor="@color/color_text_333"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:filterTouchesWhenObscured="false"
            android:text="、"
            />

        <TextView
            android:textSize="@dimen/x24"
            android:textColor="@color/color_text_333"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:filterTouchesWhenObscured="false"
            android:text="《儿童隐私保护须知》"
            />

        <TextView
            android:textSize="@dimen/x24"
            android:textColor="@color/color_text_333"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:filterTouchesWhenObscured="false"
            android:text="及"
            />
        <TextView
            android:textSize="@dimen/x24"
            android:textColor="@color/color_text_333"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:filterTouchesWhenObscured="false"
            android:text="《隐私政策》"
            />


    </com.lahm.learndaemon.view.AutoItemLayout>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值