将按钮对齐到屏幕底部

4

我有一个帧布局中的两个按钮,想要它们在屏幕底部对齐,一个在另一个下面。

使用android:layout_gravity="bottom"会让它们重叠在一起。我没有使用RelativeLayout的问题,但是RelativeLayout不支持android:layout_gravity="bottom"

我的XML布局文件

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg"
    android:orientation="vertical" >

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/scanit"
        android:layout_gravity="bottom"
        android:text="Button 1" />
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/inf"
        android:layout_gravity="bottom"
        android:text="Button 2" />
</FrameLayout>
5个回答

11

你可以使用下面的布局代码,它可能会对你有所帮助。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/mLlayoutBottomButtons" >
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/mLlayoutBottomButtons"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button2" />
    </LinearLayout>

</RelativeLayout>

在按钮中声明 android:layout_width="match_parent" 时出现错误。 - Samrat Mazumdar
请使用“wrap_content”来实现该功能。 - Dipak Keshariya
我希望按钮一个接一个地排列。 - Samrat Mazumdar

4

使用 android:gravity="bottom",并且在 RelativeLayout 中使用 android:layout_alignParentBottom="true"


2
我的理解是,如果你指定上面的其他小部件会填充额外的空间,那么你可以有一个布局/一组挂件占据屏幕底部。这是使用布局权重参数完成的。在这里,我将两个按钮放入所需方向的线性布局中。然后,在另一个布局中使用权重=1排列上面的其余小部件。具有权重1的布局增长并且不覆盖按钮。

更详细的内容请参见博客文章

效果如下图所示:screen here


1
在任何线性布局或相对布局下使用以下代码。我已经应用了权重,以平均分配底部的两个半按钮。享受吧。
<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/mLlayoutBottomButtons">
        <LinearLayout
            android:orientation="horizontal"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_gravity="bottom">
            <Button
                android:text="Cancel"
                android:layout_width="0sp"
                android:layout_weight="0.5"
                android:layout_height="wrap_content"
                android:id="@+id/button1" />
            <Button
                android:text="OK"
                android:layout_width="0sp"
                android:layout_weight="0.5"
                android:layout_height="wrap_content"
                android:id="@+id/button2" />
        </LinearLayout>
    </RelativeLayout>

1

试一下这个

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:gravity="bottom"
   >


<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Button1" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/button1" 
    android:text="Button2" />

</RelativeLayout>

以上代码运行良好,但它们不在屏幕底部。我希望它们位于屏幕底部。例如:http://3.bp.blogspot.com/-m9XTL0rL3L0/Twvfzjkkq4I/AAAAAAAAAY8/mAf39bvGses/s1600/event01.png 它们位于中心,我希望它们位于底部。 - Samrat Mazumdar
我认为你漏掉了一些东西,请粘贴我的代码。我的截图看起来像这样:http://snag.gy/CXUYL.jpg。 - Khan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接