产品需求:
布局中有两个View(或者多个),他们的宽度都不确定,那么当其中一个宽度过宽时,要保证另一个View的正常显示.
效果图:
正常情况下:
宽度过宽情况下:
思路:
1.LinearLayout 线性布局权重(可行,但需要额外嵌套一层):
给第一个控件设置宽度0,权重1,第二个控件宽度自适应:
结果:过宽状态下效果正常,但是没有过宽时第二个View在父布局最右边.
补充:在最外层再添加一层布局
2.RelativeLayout 相对布局,条件方式(不可行):
效果同线性布局.......
3.ConstraintLayout 约束布局,约束条件(完美运行):
以下是代码:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="@dimen/dp_40">
<TextView
android:id="@+id/tv_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constrainedWidth="true"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/tv_2"
app:layout_constraintHorizontal_bias="0"
android:text="生化危机366666666666666666666"
android:textColor="#ffffff"
android:singleLine="true"
android:ellipsize="end" />
<TextView
android:id="@+id/tv_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/dp_10"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@id/tv_1"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0"
android:text="生化危机3"
android:textColor="#ffffff"/>
</androidx.constraintlayout.widget.ConstraintLayout>