【发布时间】:2021-08-25 11:07:48
【问题描述】:
我在一个 LinearLayout 中有两个 TextView,我希望这两个文本都占用 LinearLayout 中给定的空间,但不会相互截断。我尝试与minWidth 合作,但layout_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
所有三个场景都应该工作
Two TextViews side by side, only one to ellipsize?
这与我的问题相似但不一样。上面的 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"/>
【问题讨论】:
标签: android textview widget overlap ellipse