首页 > 解决方案 > 有没有办法在屏幕的两端显示两个 android 按钮

问题描述

我试图让我的两个按钮显示在屏幕的两端。

这是我的 XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center">


        <ImageButton
            android:id="@+id/previous_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            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:src="@drawable/arrow_right" />


    </LinearLayout>

</FrameLayout>

这就是我想要完成的(底部箭头按钮是我想要修改的)但它们一直在左侧或中间挤在一起。(上面的代码把它们放在中间。我想要一个在左边,一个在右边。

在此处输入图像描述

标签: android

解决方案


您可以添加一个空视图并设置layout_weight如下:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:orientation=horizontal">

        <ImageButton
            android:id="@+id/previous_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/arrow_left" />

        <View
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:width="0dp"
            android:layout_weight="1" />

        <ImageButton
            android:id="@+id/next_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/arrow_right" />

    </LinearLayout>
</FrameLayout>

推荐阅读