Android 如何设置线性布局屏幕底部的按钮

Android 如何设置线性布局屏幕底部的按钮,android,android-layout,Android,Android Layout,我刚开始使用Android,有以下代码。我想在屏幕底部显示三个按钮,而不改变相对布局。我尝试设置内部线性布局的布局\重量和重力,但这会将按钮放在底部,在手机的三个菜单按钮后面。如何在菜单按钮上方显示它们?任何帮助都将不胜感激 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layo

我刚开始使用Android,有以下代码。我想在屏幕底部显示三个按钮,而不改变相对布局。我尝试设置内部线性布局的
布局\重量
重力
,但这会将按钮放在底部,在手机的三个菜单按钮后面。如何在菜单按钮上方显示它们?任何帮助都将不胜感激

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/search_box"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/search_box" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:paddingTop="80dp"
        android:text="@string/welcome"
        android:textSize="24sp" />

    <LinearLayout
        style="?android:attr/buttonBarStyle"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="bottom"
        android:orientation="horizontal">

        <Button
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/buttonB1" />

        <Button
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/buttonB2" />

        <Button
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/buttonB3" />
    </LinearLayout>
</LinearLayout>

左图显示了我想移动到底部的按钮。右图显示了我的尝试,但按钮部分位于系统菜单按钮后面


尝试在每个按钮的底部添加边距。将此添加到每个按钮布局:android:layout_marginBottom=“20dp”


如果这不起作用,请在linearlayout本身中添加底部边距

设置文本视图高度=0,重量=1。 线性布局高度=包裹内容。 会好的。试试看

试试看

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/search_box"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/search_box" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#F00"
        android:gravity="center_horizontal"
        android:paddingTop="80dp"
        android:text="@string/welcome"
        android:textSize="24sp" />

    <LinearLayout
        style="?android:attr/buttonBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FF0"
        android:orientation="horizontal">

        <Button
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/buttonB1" />

        <Button
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/buttonB2" />

        <Button
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/buttonB3" />
    </LinearLayout>
</LinearLayout>


为什么不使用相对布局?它将为您节省大量的时间和精力。您是否尝试在设备上运行此功能?我想知道是否只是预览屏幕显示了不正确的结果。他发布的图像显然不是预览,而是模拟器。@Vucko我不同意。不知道你怎么能说它是在模拟器中运行的。你明白了,这是一个预览。不知道我的眼睛一直在看什么:DIt可以工作,但在emulator上运行,它在按钮下面增加了额外的空间。想让它们出现在屏幕边缘。