美文网首页
ConstraintLayout解决左控件过长问题

ConstraintLayout解决左控件过长问题

作者: 小小亭长 | 来源:发表于2024-01-29 14:08 被阅读0次

主要重点:

  1. 控件在水平或者垂直方向需要链接在一起
  2. 左控件属性设置android:layout_width="0dp"宽度需要为0全屏,app:layout_constraintWidth_default="wrap"宽度默认自适应;
    同时现在也使用另一种方式android:layout_width="wrap_content"宽度自适应,app:layout_constrainedWidth="true"宽度被约束
  3. layout_constraintHorizontal_chainStyle 设置成packed,这样多个控制会贴紧在一起
  4. layout_constraintHorizontal_bias设置成0,在水平或者垂直方向上不进行偏移,默认0.5会进行偏移居中。

最终效果secondTextView会紧贴firstTextView,同时firstTextView过长后不会影响secondTextView的显示。

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/firstTextView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@id/secondTextView"
            android:text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
            android:ellipsize="end"
            android:lines="1"
            android:textColor="@color/black"
            app:layout_constraintWidth_default="wrap"
            app:layout_constraintHorizontal_bias="0"
            app:layout_constraintHorizontal_chainStyle="packed"/>

        <TextView
            android:id="@+id/secondTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="bbbbb"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/firstTextView"
            app:layout_constraintRight_toRightOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

相关文章

网友评论

      本文标题:ConstraintLayout解决左控件过长问题

      本文链接:https://www.haomeiwen.com/subject/svigodtx.html