Android Layout: how to save the rightmost text element and ellipse the leftmost text element as it grows?

I have a LinearLayout that contains two TextViews. Let the first TextView text be "short text" and the second TextView text be "(s)".

<LinearLayout
     android:layout_width="0dp"
     android:layout_weight="1"
     android:layout_height="match_parent"
     android:orientation="horizontal">

     <TextView
            android:id="@+id/variable-size"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:ellipsize="middle"
            android:lines="1"
            android:singleLine="true"
            android:text="short text"/>

        <TextView
            android:id="@+id/fixed-size"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:ellipsize="none"
            android:lines="1"
            android:singleLine="true"
            android:text="(s)" />

</LinearLayout>

I want LinearLayout to be displayed to the user this way:

[[short text][(s)]____________]

where ____means empty view.

Now, if I put a slightly long line in the first TextView, I want to see this:

[[slightly longer text][(s)]__]

and if I put a much longer line in the first TextView, I want to see this:

[[really long ...ng text][(s)]]

But I cannot find a way to keep the first TextView from crowding out the second TextView as a whole, for example:

[[really long lo... long text]]

How do I get the behavior I'm looking for?

+4
1

:

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

    <TextView
        android:id="@+id/variable-size"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ellipsize="middle"
        android:lines="1"
        android:singleLine="true"
        android:text="short text" />

    <TextView
        android:id="@+id/fixed-size"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:ellipsize="none"
        android:lines="1"
        android:singleLine="true"
        android:text="(s)" />

</LinearLayout>

... , LinearLayout :

android:layout_width="0dp"
android:layout_weight="1"

to

android:layout_width="wrap_content"

.

:

[[short text][(s)]]
[[slightly longer text][(s)]]
[[really long ...ng text][(s)]]

, " " (____). LinearLayout's layout_width, ((s) ), . :

[[short text            ][(s)]]
[[slightly longer text  ][(s)]]
[[really long ...ng text][(s)]]

, " " , () LinearLayout ( " " View). :

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1">

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

        <TextView
            android:id="@+id/variable-size"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:ellipsize="middle"
            android:lines="1"
            android:singleLine="true"
            android:text="short text" />

        <TextView
            android:id="@+id/fixed-size"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:ellipsize="none"
            android:lines="1"
            android:singleLine="true"
            android:text="(s)" />

    </LinearLayout>

    <View
        android:id="@+id/empty_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>

:

[[[short text][(s)]]____________]
[[[slightly longer text][(s)]]__]
[[[really long ...ng text][(s)]]]
+6

Source: https://habr.com/ru/post/1607536/


All Articles