简体   繁体   中英

Android — Layout ellipsize with right-aligned view

How can I achieve this as Text1 gets longer?

|[Text1] [Text2] _ __ _ __ _ __ _ ___ |

|[Text1 Text1 Text1] [Text2] _ ___ |

|[Text1 Text1 Text1 Tex...][Text2]|

Text2 should always be on the right of Text1, but when Text1 is too large, it is ellipsized and Text2 is right-aligned.

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

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ellipsize="end"
        android:singleLine="true"
        android:text="aaaaaaaaaaaaaaaaaaaaaaaa"
        android:textSize="30dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:text="bbbbb"
        android:textSize="30dp"/>
</LinearLayout>

I had a scenario where I needed to have it like this (pretty similar to the OP)

[[TextView1][Icon]_______[TextView2]]

[[TextView1 long..][Icon][TextView2]]

So the TextView1 can have arbitrary length and should be ellipsized if it can't fit any more, but the Icon should be right next to it and the TextView2 should always be right-alighned

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

            <FrameLayout
                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/TextView1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:singleLine="true"
                        android:ellipsize="end"
                        .... />

                    <ImageView
                        android:id="@+id/Icon"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        .... />

                </LinearLayout>

            </FrameLayout>

            <TextView
                android:id="@+id/TextView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                .... />
        </LinearLayout>

You should use RelativeLayout Like this ,

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView android:text="@string/radio_group_1"
    android:ellipsize="end" android:singleLine="true"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:layout_alignParentLeft="true" android:layout_toLeftOf="@+id/txt2"/>
<TextView android:text="@string/Pink_Floyd" android:id="@+id/txt2"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:layout_alignParentRight="true" />

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:weightSum="2" >

        <TextView
            android:id="@+id/Tv1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:singleLine="true"
            android:text="AAAAAAAAAAAAAAAAAAAAAAA"
            android:textSize="22sp"
            android:textColor="#000000"
            android:gravity="center"/>

        <TextView
            android:id="@+id/TV2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:singleLine="true"
            android:text="BBBBBBBBBBB"
            android:textColor="#000000"
            android:textSize="22sp" />


    </LinearLayout>

</RelativeLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM