我想将两个按钮与线性布局对齐,一个在左侧,一个在右侧,就像图片库中的下一个和上一个按钮一样。我试图对齐它们,但它不起作用。
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="fill_parent"
android:orientation="vertical"
android:background="@android:color/white"
android:gravity="bottom" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/black" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="prev"
android:layout_alignParentRight="true" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next" />
</LinearLayout>
</LinearLayout>
实际输出:
预期输出:
我该如何解决?
最佳答案
使用 RelativeLayout
。在那里你可以设置 android:layout_alignParentLeft
和 android:layout_alignParentRight
。
这应该适合你:
<?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="fill_parent"
android:orientation="vertical"
android:background="@android:color/white"
android:gravity="bottom" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/black" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="prev"
android:layout_alignParentLeft="true" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</LinearLayout>
关于android - 在Android中左右对齐两个按钮布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11222916/