我有一个水平 LinearLayout,有 3 个文本视图,宽度设置为wrap_content。我希望它们全部都是换行内容,但其中之一可能会变得太长。这会导致其他两个文本视图变得越界。如何使长文本视图将内容仅包装到 LinearLayout 的所有子级仍然存在的范围内(在父 LinearLayout 的宽度边界内(设置为 match_parent))?
代码示例:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="32dp"
android:orientation="horizontal"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a very very very very very very very very very long text"
android:ellipsize="end"
android:maxLines="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is another text"
android:maxLines="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="One final text"
android:ellipsize="end"
android:maxLines="1"/>
</LinearLayout>
注意:android:layout_weight 对我不起作用,因为我仍然希望文本视图为较小的文本包装内容。
将父级(LinearLayout)的宽度设置为wrap_content。但是,如果第一个文本视图的文本超出第二个文本视图的开头,这将导致第二个文本视图位于第一个文本视图的顶部。
我看到的最佳选择是为所有文本视图指定layout_width,然后为每个文本视图设置文本的最大长度。
您可以为每个
android:maxWidth
设置
android:layout_width="wrap_content"
和
TextView
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="32dp"
android:orientation="horizontal"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<TextView
android:layout_width="wrap_content"
android:maxWidth="150dp"
android:layout_height="wrap_content"
android:text="This is a very very very very very very very very very long text"
android:ellipsize="end"
android:maxLines="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="150dp"
android:text="This is another text"
android:maxLines="1"/>
<TextView
android:layout_width="wrap_content"
android:maxWidth="150dp"
android:layout_height="wrap_content"
android:text="One final text"
android:ellipsize="end"
android:maxLines="1"/>
</LinearLayout>
在 LinearLayout 中更改布局权重:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="32dp"
android:orientation="horizontal"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/teal_200"
android:text="This is a very very very very very very very very very long text"
android:ellipsize="end"
android:maxLines="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/teal_700"
android:text="This is another text"
android:maxLines="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/purple_500"
android:text="One final text"
android:ellipsize="end"
android:maxLines="1"/>
</LinearLayout>
这似乎有效
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="32dp"
android:orientation="horizontal"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/teal_200"
android:text="This is a very very very very very very very very very long text"
android:ellipsize="end"
android:maxLines="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/teal_700"
android:text="This is another text This is a very very very very very very very very very long text"
android:maxLines="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/purple_500"
android:text="One final text This is a very very very very very very very very very long text"
android:ellipsize="end"
android:maxLines="1"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
在 LinearLayout 添加这个参数:
android:weightSum="3"
在 3 个 TextView 中的每一个将 WIDTH 设置为零,如下所示:
android:layout_width="0dp"
并像这样添加权重 1:
android:layout_weight="1"
如果您有 2 个“TextView”,则“weightSum”应为 2,如果您有 4 个,则“weightSum”应为 4。
这将使布局均匀分布。