java 如何在不与 TextView 重叠的情况下将 Button 向右对齐?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2720719/
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
How to align Button to the right, without being overlapped by TextView?
提问by benvd
I'm trying to get something like this: http://img202.imageshack.us/img202/552/layoutoy.png. I'm using this as a list item (technically as the group view of an ExpandableListView).
我想得到这样的东西:http://img202.imageshack.us/img202/552/layoutoy.png。我将它用作列表项(技术上作为 ExpandableListView 的组视图)。
Here's the XML file:
这是 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight">
<TextView
android:id="@+id/list_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end" />
<Button
android:id="@+id/list_item_button"
android:text="Click me!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="@id/list_item_text" />
</RelativeLayout>
But this doesn't work. The Button doesn't wrap its contents, instead it uses all available horizontal space. The TextView doeswrap its contents, but what I want it to do is to cut off when it overlaps the Button.
但这不起作用。Button 不包装其内容,而是使用所有可用的水平空间。TextView确实包装了它的内容,但我想要它做的是在它与按钮重叠时切断。
In other words, I want all the buttons to be of the same width, regardless of the amount of text in the textviews. Is this at all possible?
换句话说,我希望所有按钮都具有相同的宽度,而不管 textviews 中的文本数量。这是可能吗?
采纳答案by Janusz
I think you should try it the other way round. Make the TextView be to the left of the button. This way the Textview won't overlap the Button. If you want it to be cut of at the end of the line you have to constrain it to one line. At the moment it would just move the rest of the text to the next line.
我认为你应该反过来试试。使 TextView 位于按钮的左侧。这样 Textview 就不会与按钮重叠。如果您希望在该行的末尾将其剪掉,则必须将其限制为一行。目前它只会将文本的其余部分移动到下一行。
This should do the trick:
这应该可以解决问题:
<Button
android:id="@+id/list_item_button"
android:text="Click me!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"/>
<TextView
android:id="@+id/list_item_text"
android:text="veryveryveryveryveryveryveryveryveryverylong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/list_item_button"
android:ellipsize="end" />
</RelativeLayout>
回答by Andras Balázs Lajtha
Before anyone tries the method shown above (with the RelativeLayout), you should use LinearLayout for this. The weights should be set correctly: the element that needs to take up the empty space has to have a weight of 1 and width set to fill_parent, the element that needs to keep its minimal size has to have a weight of 0 with a width of wrap_content.
在任何人尝试上面显示的方法(使用 RelativeLayout)之前,您应该为此使用 LinearLayout。应该正确设置权重:需要占用空白空间的元素的权重必须为 1,宽度设置为 fill_parent,需要保持其最小尺寸的元素的权重必须为 0,宽度为包装内容。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight">
<TextView
android:id="@+id/list_item_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight=1
android:ellipsize="end" />
<Button
android:id="@+id/list_item_button"
android:text="Click me!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=0/>
</LinearLayout>

