在 Android 中将 ImageButtons 放置在 LinearLayout 的角落
Placement of ImageButtons in corners of LinearLayout in Android
我想把两个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
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>
相对布局更适合在屏幕边缘定位视图
有另一种方法可以代替 RelativeLayout
通过使用第三个视图,布局 xml 代码:
<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
更好,而且布局更流畅
我想把两个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
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>
相对布局更适合在屏幕边缘定位视图
有另一种方法可以代替 RelativeLayout
通过使用第三个视图,布局 xml 代码:
<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
更好,而且布局更流畅