简体   繁体   English

Android - 使用右对齐视图进行布局椭圆化

[英]Android — Layout ellipsize with right-aligned view

How can I achieve this as Text1 gets longer? 当Text1变长时,我怎样才能实现这一目标?

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

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

|[Text1 Text1 Text1 Tex...][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. Text2应该始终位于Text1的右侧,但是当Text1太大时,它会被椭化并且Text2是右对齐的。

<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) 我有一个场景,我需要像这样(非常类似于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 因此, TextView1可以具有任意长度,如果它不能再适合,则应该进行椭圆化,但是Icon应该在它旁边, TextView2应该总是右对齐

<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 , 你应该使用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">

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android - 具有右对齐视图的Ellipsize - Android - Ellipsize with right aligned view 在Android上列出布局,如何使按钮右对齐? - List Layout on Android, how to make buttons right-aligned? 当屏幕宽度较小时,如何在右对齐按钮旁边省略居中对齐的文本视图 - How to ellipsize center-aligned textview beside a right-aligned button when screen width is small Android-如何强制微调框下拉列表打开右对齐 - Android - How to force spinner dropdown to open right-aligned 带有右对齐检查按钮/框的Android CheckBox - Android CheckBox with right-aligned checking button/box 具有右对齐,裁剪,未缩放内容的ImageView - ImageView with right-aligned, cropped, unscaled content 如何为有一个TextView左对齐和一个TextView右对齐的ListView行创建XML布局? - How do I make an XML layout for a ListView row where there is a TextView left-aligned and a TextView right-aligned? 如何在不重叠滚动条的情况下显示 Android 右对齐 ListItem 数据? - How to show Android Right-aligned ListItem data without overlapping the scrollbar? 如何布局视图右对齐和 LinearLayout 的底部 - How to layout view right aligned and bottom of an LinearLayout ListView行样式 - 左对齐文本和右对齐图标 - ListView row styling - left aligned text, and right-aligned icon
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM