[英]Placement of ImageButtons in corners of LinearLayout in Android
I am trying to place two ImageButton
in the corners of a LinearLayout
as shown in figure below: 我试图将两个
ImageButton
放置在LinearLayout
的角落,如下图所示:
I have tried using the android:layout_gravity
attribute with the value set to left
and right
for the two buttons. 我尝试使用
android:layout_gravity
属性,将两个按钮的值分别设置为left
和right
。 However, they show just next to each other as shown in image below: 但是,它们彼此相邻显示,如下图所示:
How can I place the two image buttons in the corners of the LinearLayout
? 如何将两个图像按钮放在
LinearLayout
的角上? I have looked up a lot of previous answers and tried them but nothing seems to work. 我查询了很多以前的答案并尝试过,但似乎没有任何效果。 How can this placement of image buttons be done?
如何放置图像按钮?
The XML of the layout is in the snippet below: 布局的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>
Use a RelativeLayout: 使用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>
A relativelayout is better for positioning views at the edges of the screen 相对布局更适合将视图放置在屏幕边缘
there is another way to do this instead of using RelativeLayout
by using a third View , layout xml code: 还有另一种方法,而不是通过使用第三个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>
this way is better than using RelativeLayout
and makes your layout smoother 这种方法比使用
RelativeLayout
更好,并使布局更平滑
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.