简体   繁体   中英

Align Button to the bottom of screen

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"

My XML Layout file

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

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>

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

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.

blog post showing this in more detail .

This will look as below 屏幕在这里

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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