Java LinearLayout:对齐到父级的右侧
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20031846/
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
LinearLayout: Aligning to the Right of the parent
提问by Lemon Juice
Pleae have a look at the following XML
请查看以下 XML
<?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" >
<TextView
android:id="@+id/open_file_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:gravity="left"
android:padding="5dp"
android:text="TextView" />
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="@string/open" />
</LinearLayout>
I want to align the Button
to the right corner of the layout. How can I do this?
我想将Button
与布局的右角对齐。我怎样才能做到这一点?
采纳答案by Manishika
Use RelativeLayout
for this android:layout_alignParentLeft="true"
and android:layout_alignParentRight="true"
使用RelativeLayout
这种android:layout_alignParentLeft="true"
和android:layout_alignParentRight="true"
Try below data
试试下面的数据
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/open_file_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:padding="5dp"
android:text="TextView" />
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="@string/open" />
</RelativeLayout>
回答by Blacklight
You can add a filling View between the TextView
and the Button
using the layout_weight
attribute:
您可以使用属性在TextView
和之间添加填充视图:Button
layout_weight
<View android:layout_width="0dp"
android:layout_heigth="wrap_content"
android:layout_weight="1"/>
Or better use a RelativeLayout
and android:layout_alignParentRight="true"
.
或者更好地使用 aRelativeLayout
和android:layout_alignParentRight="true"
。
回答by wvdz
Another possible way of doing this is by using the layout_weight attribute. This has my preference, because it fills up all available space.
另一种可能的方法是使用 layout_weight 属性。这是我的偏好,因为它填满了所有可用空间。
<?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" >
<TextView
android:id="@+id/open_file_name"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="match_parent"
android:layout_gravity="start|left"
android:gravity="start|left"
android:padding="5dp"
android:text="TextView" />
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="end|right"
android:text="open" />
</LinearLayout>
回答by jagmohan
Try this as well.
也试试这个。
<?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" >
<TextView
android:id="@+id/open_file_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:layout_gravity="left"
android:padding="5dp"
android:text="TextView" />
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="@string/open" />
</LinearLayout>
回答by vinoth h
Just try this,it worked for me.
试试这个,它对我有用。
<TextView
android:id="@+id/open_file_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:gravity="left"
android:padding="5dp"
android:text="TextView" />
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="@string/open" />
</RelativeLayout>
</LinearLayout>