如何使 wrap_content 文本视图仅将其内容包裹起来,以使其他视图在 LinearLayout android 中仍然可见?

我有一个带有 3 个文本视图的水平 LinearLayout,宽度设置为 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>

这是我得到的: undesired

这更多是我想要的: desired

注意:android:layout_weight 对我不起作用,因为我仍然希望文本视图为较小的文本包装内容。

MELIDY 回答:如何使 wrap_content 文本视图仅将其内容包裹起来,以使其他视图在 LinearLayout android 中仍然可见?

将父级 (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>

enter image description here

,

在您的 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>


enter image description here

本文链接:https://www.f2er.com/15746.html

大家都在问