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>