开发者

Android -- Layout ellipsize with right-aligned view

开发者 https://www.devze.com 2023-04-11 16:05 出处:网络
How can I achieve this as Text1 gets longer? [Text1] [Text2]_____________| [Text1 Text1 Text1] [Text2]____|

How can I achieve this as Text1 gets longer?

|[Text1] [Text2]_____________|

|[Text1 Text1 Text1] [Text2]____|

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

Text2 should 开发者_JAVA百科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>
0

精彩评论

暂无评论...
验证码 换一张
取 消