如何在UI底部的固定位置设置按钮?

Adh*_*ham 9 android

我想要一个按钮一直出现在固定位置,在UI的页脚?(总是,如果它上面有组件或者没有)

Chi*_*rag 28

请在主布局下选择一个相对布局.将其高度和宽度设置为填充父级,并将其重力设置为底部,并将任何textview或任何按钮放入其中.

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

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

        <Button 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:text="Bottom Gravity" />

    </RelativeLayout>

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <Button 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:text="Without Gravity" />

    </LinearLayout>

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Mar*_*ues 6

这取决于您使用的布局.

在RelativeLayout上有

android:layout_alignParentBottom="true"
Run Code Online (Sandbox Code Playgroud)

在LinearLayout上,将其放在底部,并确保正确设置元素layout_weight.

此外,检查财产

android:layout_gravity
Run Code Online (Sandbox Code Playgroud)

并注意到它不同于

android:gravity
Run Code Online (Sandbox Code Playgroud)