微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何使 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 对我不起作用,因为我仍然希望文本视图为较小的文本包装内容

解决方法

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。