问题
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>
回答1:
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>
回答2:
use android:gravity="bottom"
and with relative layout
use android:layout_alignParentBottom="true"
回答3:
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

回答4:
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>
回答5:
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>
来源:https://stackoverflow.com/questions/11272106/align-button-to-the-bottom-of-screen