Android 将按钮对齐到屏幕底部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11272106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:37:28  来源:igfitidea点击:

Align Button to the bottom of screen

androidxmlandroid-layoutandroid-linearlayoutrelativelayout

提问by Samrat Mazumdar

I have two buttons in a frame layout and want them to align at the bottom of screen one below the another.

我在框架布局中有两个按钮,并希望它们在屏幕底部对齐,一个在另一个下方。

Using android:layout_gravity="bottom"makes them overlap over each other. I have no issues doing it with Relativelayout but relativelayout doesn't supports android:layout_gravity="bottom"

使用android:layout_gravity="bottom"使它们相互重叠。我用相对布局做这件事没有问题,但相对布局不支持android:layout_gravity="bottom"

My XML Layout file

我的 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>

回答by Dipak Keshariya

Use Below Layout Code for that, it may help you.

为此使用下面的布局代码,它可能对您有所帮助。

<?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>

回答by Vineet Shukla

use android:gravity="bottom"and with relative layoutuse android:layout_alignParentBottom="true"

使用android:gravity="bottom"relative layout使用android:layout_alignParentBottom="true"

回答by quiet_penguin

My understanding is that you can have a layout/set of widgets to occupy the bottom of the screen provided you specify that the other widgets above will be filling the extra space. This is done using layout weight parameter. Here I put the two buttons in to a linearlayout with orientation as required. Then the rest of the widgets above in another layout with weight = 1. The layout with weight 1 growns and does not cover the buttons.

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

blog post showing this in more detail.

博客文章更详细地展示了这一点

This will look as below screen here

这将如下所示 屏幕在这里

回答by Mayank Gaur

Use Below Code below any Linear Or Relative Layout.I have applied weights to equally divide buttons in two half at bottom. Njoy

在任何线性或相对布局下方使用下面的代码。我已应用权重将底部的按钮等分为两半。恩乔伊

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

回答by Khan

try this

尝试这个

<?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>