[英]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.