【问题标题】:Two TextViews side by side - both TextViews should take always as much space as possible两个 TextView 并排 - 两个 TextView 应始终占用尽可能多的空间
【发布时间】: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


    【解决方案1】:

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

    <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>
    

    【讨论】:

    • 此代码几乎是正确的,谢谢。我有一个解决方案。它现在在我的问题中!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多