简体   繁体   English

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

[英]how to align a set of button to bottom of the screen in android

In my layout there are four sections respectively a header, an editable control, a list and a set of button. 在我的布局中,有四个部分分别是标题,可编辑控件,列表和一组按钮。 I want to keep the buttons bottom of the screen. 我想将按钮保持在屏幕下方。 I make too many changes in the layout to force the buttons to bottom. 我在布局上做了太多更改以强制按钮到底。 But nothing happened. 但什么都没发生。 Please provide instructions to make what i needed. 请提供说明,以满足我的需求。 I am posting the layout also 我也在发布布局

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

在此输入图像描述

Make the Parent of the LinearLayout containing the button as relative and set the property of linear layout containing two buttons as android:layout_alignParentBottom="true". 将包含按钮的LinearLayout的Parent设置为relative,并将包含两个按钮的线性布局的属性设置为android:layout_alignParentBottom =“true”。 You can also set the gravity of the Linear Layout to Bottom. 您还可以将“线性布局”的重力设置为“底部”。

Try this: 尝试这个:

<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:layout_alignParentBottom="true"赋予按钮的布局......

您必须使用父布局作为亲属,然后使用属性android:layout_alignParentBottom =“true”进行子布局。

Relative layout is the best way to do it as Karan_Rana suggested, but if you dont want to use relative layout try this one: 相对布局是Karan_Rana建议的最佳方式,但如果您不想使用相对布局,请尝试以下方法:

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

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

    <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="fill_parent" />

</LinearLayout>
</ScrollView>

 <!-- Save & Cancel button -->
         <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:layout_weight="0"
        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>

Here, you need to use LinearLayout as parent and set weightSum to it and apply the same layout_weight to the layout you want in fullscreen. 在这里,您需要使用LinearLayout作为父级并weightSum设置weightSum ,并将相同的layout_weight应用于全屏所需的布局。 And the layout which you want in bottom(save and cancel) apply layout_weight to 0. 您想要在底部(保存和取消)的布局将layout_weight应用于0。

This works well! 这很好用!

Try out this code by making relative layout as parent layout and android:layout_alignParentBottom="true" to your button layout 通过将相对布局作为父布局和android:layout_alignParentBottom =“true”到您的按钮布局来试用这段代码

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

<!-- heading for basic settings -->

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

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

        <!-- macro name -->

        <LinearLayout
            android:id="@+id/llTop"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingLeft="20dp"
            android:paddingRight="20dp" >

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="left|center_vertical"
                android:layout_weight="1"
                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:inputType="textCapCharacters"
                android:maxLength="12"
                android:singleLine="true" />
        </LinearLayout>

        <!-- Save & Cancel button -->

    </RelativeLayout>
</ScrollView>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    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>

try this. 试试这个。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    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:layout_alignParentRight="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true" >

        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginRight="2dp"
            android:text="Modify" />

        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginRight="2dp"
            android:text="Cancle" />

    </LinearLayout>

</RelativeLayout>

for display button at bottom. 用于底部的显示按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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