标签跟随文字控件的实现

149 阅读1分钟

image.png

使用协调布局实现

主要依赖chain特性 layout_constrainedWidth layout_constraintHorizontal_bias layout_constraintHorizontal_chainStyle 这三个属性

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/cl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:maxLines="1"
            android:text="右边需要跟一个标签"
            app:layout_constrainedWidth="true"
            app:layout_constraintBottom_toBottomOf="@+id/tv2"
            app:layout_constraintHorizontal_bias="0"
            app:layout_constraintHorizontal_chainStyle="packed"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@id/tv2"
            app:layout_constraintTop_toTopOf="@id/tv2" />

        <TextView
            android:id="@+id/tv2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:background="#ff0000"
            android:padding="3dp"
            android:text="标签"
            android:textColor="#fff"
            app:layout_constraintLeft_toRightOf="@id/tv"
            app:layout_constraintRight_toRightOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

自定义控件实现

自定义父控件,从后往前测量控件的宽度, 前边的宽度约束为剩下空间的宽度


public class TextAndTagLayout extends ViewGroup {
    private int space;
    public TextAndTagLayout(@NonNull Context context) {
        super(context);
        init();
    }

    public TextAndTagLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TextAndTagLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        space = 30;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec)-getPaddingLeft()-getPaddingRight();
        int height= 0;
        for (int i = getChildCount()-1; i >= 0; i--) {
            if(getChildAt(i).getVisibility() != GONE){
                measureChild(getChildAt(i),MeasureSpec.makeMeasureSpec(width,MeasureSpec.EXACTLY),MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(heightMeasureSpec),MeasureSpec.EXACTLY));
                int childWidth = getChildAt(i).getMeasuredWidth();
                int childHeight = getChildAt(i).getMeasuredHeight();
                height = Math.max(childHeight,height);
                width -= (childWidth+space);
            }
        }
        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),height);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        int childLeft = getPaddingLeft();
        for (int i = 0; i < getChildCount(); i++) {
            View childView = getChildAt(i);
            if(childView.getVisibility() != GONE){
                int childTop = getPaddingTop() + (getMeasuredHeight() - childView.getMeasuredHeight()) / 2;
                childView.layout(childLeft,childTop,childLeft+childView.getMeasuredWidth(),childTop+childView.getMeasuredHeight());
                childLeft += (childView.getMeasuredWidth()+space);
            }
        }
    }
}