在Android中的LinearLayout的角落中放置ImageButtons

问题描述 投票:0回答:2

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

enter image description here

我尝试使用android:layout_gravity属性,将值设置为leftright作为两个按钮。但是,它们显示在彼此旁边,如下图所示:

enter image description here

如何将两个图像按钮放在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>
android android-linearlayout android-imagebutton
2个回答
2
投票

使用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>

相对布局更适合在屏幕边缘定位视图


0
投票

还有另一种方法可以通过使用第三个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更好,使您的布局更流畅

© www.soinside.com 2019 - 2024. All rights reserved.