【发布时间】:2020-02-10 13:25:01
【问题描述】:
我的布局上有三个视图 TextView1 TextView2 和 ImageView。
TextView1 包裹内容,而 TextView2 与 TextView1 的末尾对齐
TextView1 TextView2 ImageView TextView1 TextView1 ImageView
TextView1 TextView1 > 一起填满了布局的所有宽度
我已经尝试了很多选项来使用 LinearLayout、RelativeLayout、ConstraintLayout 在 xml 中创建它,但它们都没有给我想要的结果。
最近的是
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/card_item_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:maxLines="1"
android:ellipsize="end"
android:gravity="start"
app:layout_constraintWidth_max="284dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="WWWWWWWWWWWWWWWWWWWWWWW"/>
<TextView
android:id="@+id/card_item_small_number"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingStart="@dimen/offset_6"
android:paddingEnd="@dimen/offset_6"
android:maxLines="1"
app:layout_constraintStart_toEndOf="@id/card_item_name"
app:layout_constraintEnd_toStartOf="@id/card_item_bank_logo"
app:layout_constraintTop_toTopOf="parent"
android:gravity="start"
tools:text="**** 5555"/>
<ImageView
android:id="@+id/card_item_bank_logo"
android:layout_width="wrap_content"
android:layout_height="@dimen/offset_24"
android:maxHeight="@dimen/offset_24"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@drawable/ic_language"/>
</androidx.constraintlayout.widget.ConstraintLayout>
这里的关键在于 TextView1 属性
android:layout_width="0dp"
app:layout_constraintWidth_max="284dp"
但这意味着我必须在运行时计算 layout_constraintWidth_max 值,因为 TextView2 和 ImageView 的宽度不是固定的,取决于它的价值观
有没有办法只在没有“魔术”数字的 xml 中制作它?
【问题讨论】:
-
你试过为 textview1 设置约束结束吗?所以 tv1 endToStart 的 tv2 并删除最大宽度
-
@P.Juni 是的,我有。在这种情况下,TextView1 将 match_constraint 而不是 wrap_contant。这会导致 TextView1 填充所有可用空间