android - 如何防止 "wrap_content" TextView 将另一个 View 推到看不见的地方?

标签 android

我在一个布局中有两个 TextView 。我想像这样将它们并排显示:

|[TextView1][TextView2] |

第一个 TextView 的宽度可以有很大的不同。从几个符号到 100 个符号,但如果它太长我想将它省略,并且 TextView2 应该始终可见。像这样:

|[TextView1TextView1Tex...][TextView2]|

我尝试了以下布局,但它始终在屏幕末尾显示 TextView2:

[TextView1] [TextView2]|

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:singleLine="true"
            android:maxLines="1"
            android:ellipsize="end"/>

        <TextView
            android:layout_marginLeft="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>

我曾尝试将 TextView1 的宽度从 0 更改为 wrap_content,但它会像这样将 TextView2 推到视线之外:

|[TextView1TextView1TextView1Text...]|

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:maxLines="1"
            android:ellipsize="end"/>

        <TextView
            android:layout_marginLeft="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>

如有任何帮助,我们将不胜感激。

最佳答案

这可能有点矫枉过正,但 TableLayout 可以解决问题。

<TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:shrinkColumns="0">

        <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            <TextView
                    android:id="@+id/firstTextView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:maxLines="1"
                    android:ellipsize="end"/>

            <TextView
                    android:id="@+id/secondTextView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:maxLines="1"/>

        </TableRow>
    </TableLayout>

android:shrinkColumns="0" 是这里的关键。它告诉表格只缩小第一列,同时保持其他列不变。结果是系统只会调整第一列的大小,即您的第一个 TextView。

关于android - 如何防止 "wrap_content" TextView 将另一个 View 推到看不见的地方?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28824076/

相关文章:

android - 使用新语法应用 Android Gradle 插件

android - 更改持续时间android音频播放器

android - 自定义构建类型不适用于 androidTest 构建

android - TextView 回车不起作用

android - 在 Android 上呈现 Kannada(印度语)文本

java - 我最初如何使用 EthereumJ 连接到 Android 中的以太坊网络?

android - Dagger 2 如何让 Android 上的测试更容易?

android - 如何取消此重复警报?

android - android 中谷歌地图中的自定义标记

Android Google Maps API OnLocationChanged 仅调用一次