我试图在ImageButton
的角落放置两个LinearLayout
,如下图所示:
我尝试使用android:layout_gravity
属性,将值设置为left
和right
作为两个按钮。但是,它们显示在彼此旁边,如下图所示:
如何将两个图像按钮放在LinearLayout
的角落?我查了很多以前的答案并尝试过但似乎没有任何效果。如何放置图像按钮?
布局的XML位于以下代码段中:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|fill_horizontal"
android:orientation="horizontal">
<ImageButton
android:id="@+id/prev_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:contentDescription="@string/prev_button"
android:src="@drawable/arrow_left"/>
<ImageButton
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:contentDescription="@string/next_button"
android:src="@drawable/arrow_right"/>
</LinearLayout>
使用RelativeLayout:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/prev_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:contentDescription="@string/prev_button"
android:src="@drawable/arrow_left"/>
<ImageButton
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:contentDescription="@string/next_button"
android:src="@drawable/arrow_right"/>
</RelativeLayout>
相对布局更适合在屏幕边缘定位视图
还有另一种方法可以通过使用第三个View布局xml代码来代替使用RelativeLayout
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<View
android:layout_width="0dp"
android:layout_weigth="1"
android:layout_height="0.5dp"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
这种方式比使用RelativeLayout
更好,使您的布局更流畅