如何在android中将一组按钮对齐到屏幕底部

在我的布局中,有四个部分分别是标题,可编辑控件,列表和一组按钮.我想将按钮保持在屏幕下方.我在布局上做了太多更改以强制按钮到底.但什么都没发生.请提供说明,以满足我的需求.我也在发布布局
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <!--  heading for basic settings -->
        <TextView
            android:id="@+id/setup_macroheading"
            style="@style/txtHeading" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="20dp"/>

         <!--  macro name -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:orientation="horizontal">

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="left|center_vertical"
                android:layout_weight="1"
                android:textColor="@color/white"                    
                android:text="Macro Name"/>

            <EditText
                android:id="@+id/setup_macroname"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_weight="1"
                android:maxLength="12"
                android:inputType="textCapCharacters"
                android:singleLine="true"/>                 
        </LinearLayout>

        <View
            android:layout_width="fill_parent"
            android:layout_height="20dp"/>

        <ListView
            android:id="@+id/lvMacroList"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
        />

        <!-- Save & Cancel button -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:layout_gravity="bottom"
            android:orientation="horizontal">

            <Button
                android:id="@+id/setup_macroSavebtn"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:layout_weight="1"
                android:onClick="onSaveButtonClick"
                android:text="Save"/>

            <Button
                android:id="@+id/setup_macroCancelbtn"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_weight="1"
                android:onClick="onCancelButtonClick"
                android:text="Cancel"/>         
        </LinearLayout>
    </LinearLayout>
  </ScrollView>

解决方法

将包含按钮的LinearLayout的Parent设置为relative,并将包含两个按钮的线性布局的属性设置为android:layout_alignParentBottom =“true”.您还可以将“线性布局”的重力设置为“底部”.

试试这个:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

      <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <!--  heading for basic settings -->
    <TextView
        android:id="@+id/setup_macroheading"
        style="@style/txtHeading" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="20dp"/>

     <!--  macro name -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical"
            android:layout_weight="1"
            android:textColor="@color/white"                    
            android:text="Macro Name"/>

        <EditText
            android:id="@+id/setup_macroname"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_weight="1"
            android:maxLength="12"
            android:inputType="textCapCharacters"
            android:singleLine="true"/>                 
    </LinearLayout>

    <View
        android:layout_width="fill_parent"
        android:layout_height="20dp"/>

    <ListView
        android:id="@+id/lvMacroList"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
    />
</LinearLayout>
    <!-- Save & Cancel button -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:orientation="horizontal"
        android:paddingLeft="20dp"
        android:paddingRight="20dp" >

        <Button
            android:id="@+id/setup_macroSavebtn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_weight="1"
            android:onClick="onSaveButtonClick"
            android:text="Save"/>

        <Button
            android:id="@+id/setup_macroCancelbtn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_weight="1"
            android:onClick="onCancelButtonClick"
            android:text="Cancel"/>         
    </LinearLayout>
</RelativeLayout>

相关文章

Android 如何解决dialog弹出时无法捕捉Activity的back事件 在...
Android实现自定义带文字和图片的Button 在Android开发中经常...
Android 关于长按back键退出应用程序的实现最近在做一个Andr...
android自带的时间选择器只能精确到分,但是对于某些应用要求...