java 使用截断文本的线性或相对布局左右对齐?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3481591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Aligning right and left with linear or relativelayout with truncated text?
提问by droidgren
I am trying to do a listview which looks like this:

(source: pici.se)
我正在尝试做一个看起来像这样的列表视图:(
来源:pici.se)
It has two parts, the left is left-aligned and contains two textviews which are single line and truncated. The right is part also has two textviews but they are right-aligned and have constant size. The main problem is that the right part does not align to the right. If I use a static width to push it to the right side it works pretty good, but then it does not look good in landscape mode.
它有两部分,左边是左对齐的,包含两个单行和截断的文本视图。右侧部分也有两个文本视图,但它们右对齐并且大小恒定。主要问题是右侧部分未对齐。如果我使用静态宽度将其推到右侧,则效果很好,但在横向模式下看起来不太好。
I thought it should be simple to accomplish. But it wasn't.
我认为这应该很容易实现。但事实并非如此。
First I tried with Linearlayout:
首先我尝试使用 Linearlayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:orientation="vertical"
android:id="@+id/L1"
android:layout_height="fill_parent"
android:layout_weight="0.8"
android:layout_width="0dip">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="1"
android:ellipsize="end"
android:scrollHorizontally="true"
android:text="Long text Long text Long text Long text Long text Long text Long text" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="1"
android:ellipsize="end"
android:scrollHorizontally="true"
android:text="Short text" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:id="@+id/L2"
android:layout_gravity="top|right"
android:layout_height="fill_parent"
android:layout_weight="0.2"
android:layout_width="0dip">
<TextView
android:id="@+id/text4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="12:12" />
<TextView
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="64%"
android:layout_gravity="right" />
</LinearLayout>
</LinearLayout>
I have been fiddling with the weight property, but it doesn't seem to help. Right part does not align to the right at all.
我一直在摆弄重量属性,但它似乎没有帮助。右侧部分根本不与右侧对齐。
And then I tried with Relativelayout:
然后我尝试使用Relativelayout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView
android:id="@+id/text1"
android:layout_height="wrap_content"
android:lines="1"
android:scrollHorizontally="true"
android:layout_alignParentTop="true"
android:text="Long text Long text Long text Long text Long text Long text Long text"
android:layout_alignParentLeft="true"
android:ellipsize="end"
android:layout_width="275dp" />
<TextView
android:id="@+id/text2"
android:layout_height="wrap_content"
android:lines="1"
android:ellipsize="end"
android:scrollHorizontally="true"
android:text="Long text Long text Long text Long text Long text Long text Long text"
android:layout_below="@+id/text1"
android:layout_alignParentLeft="true"
android:layout_width="275dp" />
<TextView
android:id="@+id/text3"
android:layout_height="wrap_content"
android:text="12:12"
android:layout_gravity="right"
android:layout_alignParentRight="true"
android:layout_alignBaseline="@+id/text1"
android:layout_width="40dp"
android:layout_alignRight="@+id/text4" />
<TextView
android:id="@+id/text4"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="64%"
android:layout_alignParentRight="true"
android:layout_alignBaseline="@+id/text2"
android:layout_width="40dp" />
</RelativeLayout>
Same problem here, the right part is not aligning to the the right properly, and I have to use a static size of android:layout_width to push it to the other side. And if don't use a static size the text start clipping each other.
同样的问题,右边的部分没有正确对齐,我必须使用 android:layout_width 的静态大小将它推到另一边。如果不使用静态大小,文本就会开始相互剪裁。
回答by Macarse
I have the same issue as you a few days ago. Some points:
我前几天和你有同样的问题。几点:
I've used
android:singleLine="true"instead ofandroid:lines="1". Honestly I don't know the difference. I guess once is deprecated.To align right:
android:gravity="right"To avoid the overlap I placed your TextViews on the left inside a
LinearLayoutand I add itandroid:layout_marginRight="50dp"
我用过
android:singleLine="true"而不是android:lines="1". 老实说,我不知道有什么区别。我想曾经被弃用。右对齐:
android:gravity="right"为了避免重叠,我将您的 TextViews 放在 a 的左侧
LinearLayout并添加它android:layout_marginRight="50dp"
My xml:
我的xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<LinearLayout android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_marginRight="50dp">
<TextView
android:id="@+id/text1"
android:layout_height="wrap_content"
android:singleLine="true"
android:scrollHorizontally="true"
android:layout_alignParentTop="true"
android:text="Long text Long text Long text Long text Long text Long text Long text"
android:layout_alignParentLeft="true"
android:ellipsize="end"
android:layout_width="275dp" />
<TextView
android:id="@+id/text2"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="end"
android:scrollHorizontally="true"
android:text="Long text Long text Long text Long text Long text Long text Long text"
android:layout_below="@+id/text1"
android:layout_alignParentLeft="true"
android:layout_width="275dp" />
</LinearLayout>
<TextView
android:id="@+id/text3"
android:layout_height="wrap_content"
android:text="12:12"
android:layout_gravity="right"
android:layout_alignParentRight="true"
android:layout_alignBaseline="@+id/text1"
android:layout_width="40dp"
android:layout_alignRight="@+id/text4" />
<TextView
android:id="@+id/text4"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="64%"
android:layout_alignParentRight="true"
android:gravity="right"
android:layout_below="@id/text3"
android:layout_width="40dp" />
</RelativeLayout>
Using android:layout_marginRight="50dp"to avoid overlapping doesn't feel like the right choice, but at the moment is the only solution I've found.
使用android:layout_marginRight="50dp"来避免重叠感觉不是正确的选择,但目前是我找到的唯一解决方案。

