首页 > 解决方案 > 两个使用 XML 的具有动态宽度的第一个 TextView 的水平 TextView

问题描述

我想在 2 个 TextView 的位置构建布局。

第一个 TextView 的长度未定义(?)。所以它必须是单线和椭圆形结束。

第二个 TextView 已定义宽度 (wrap_content)。

我需要第二个 textview 附加到第一个 textview 的末尾,但是如果第一个 textview 的内容长度太大,它应该被省略。

此外,第二个 textview 必须留在父容器的末尾。

我知道这有点令人困惑。看看下面的图片。

在此处输入图像描述

在此处输入图像描述

可以不用 java/kotlin 编码,只用简单的 xml 来完成吗?

标签: androidandroid-layout

解决方案


这是一个解决方案LinearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="1"
        android:layout_weight="1"
        tools:text="Long" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:text="Short" />

</LinearLayout>

推荐阅读