繁体   English   中英

在Android的LinearLayout的角落放置ImageButtons

[英]Placement of ImageButtons in corners of LinearLayout in Android

我试图将两个ImageButton放置在LinearLayout的角落,如下图所示:

在此处输入图片说明

我尝试使用android:layout_gravity属性,将两个按钮的值分别设置为leftright 但是,它们彼此相邻显示,如下图所示:

在此处输入图片说明

如何将两个图像按钮放在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更好,并使布局更平滑

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM