首页 > 解决方案 > 两个 TextView 并排 - 两个 TextView 都应该尽可能多地占用空间

问题描述

我在一个 LinearLayout 中有两个 TextView,我希望这两个文本都占用 LinearLayout 中给定的空间,但不会相互切断。我试图与之合作,minWidthlayout_weight忽略了它。这是一个例子:

<TextView
        android:id="@+id/textOrigin"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:ellipsize="end"
        android:maxLines="1"
        android:layout_weight="1"
        android:minWidth="170dp".  <-- does not work.../>

<TextView
        android:id="@+id/textDestination"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:ellipsize="end"
        android:maxLines="1"
        android:layout_weight="0"
        android:gravity="end"/>

问题是第二个 TextView 可能会很长,如果宽度是 wrap_content 会切断对齐的第一个 TextView。所以我想设置一个minWidth。

|<TextView>......<TextView>| --> no cut off normal position

|<TextViewcanbeverylong...><TextView>| --> left one very long. right one should have minWidth of 100 and then ellipsized

|<TextView><TextViewcanbeverylongtoo...>| --> right one very long. left one should have minWidth of 100 and then ellipsized

所有三种情况都应该有效

两个TextView并排,只有一个椭圆?

这与我的问题相似但不一样。上面的 Ticket 只是解决了在一个方向上不切断文本,所以只有当左边的文本很长时,右边的文本才不会被切断,但我希望在两个方向上都这样。

解决方案: 这是解决方案:只需将 Width 更改为 wrap_content。然后可以使用 minWidth。

<TextView
        android:id="@+id/textOrigin"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:ellipsize="end"
        android:maxLines="1"
        android:layout_weight="1"
        android:minWidth="170dp"/>

<TextView
        android:id="@+id/textDestination"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:ellipsize="end"
        android:maxLines="1"
        android:layout_weight="1"
        android:minWidth="170dp"
        android:gravity="end"/>

标签: androidtextviewwidgetoverlapellipse

解决方案


试试这个代码,也许它有效!

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

    <TextView
        android:id="@+id/textOrigin"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="1"
        android:layout_weight="0.5"/>

    <TextView
        android:id="@+id/textDestination"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="1"
        android:layout_weight="0.5"
        android:gravity="end"/>
</LinearLayout>

推荐阅读