简体   繁体   English

如何使用线性布局将按钮设置到屏幕底部

[英]How to set buttons to the bottom of screen with Linear layout

I just started with Android and have the following code. 我刚开始使用Android并拥有以下代码。 I want to show the three buttons at the bottom of the screen without changing to a Relative layout. 我想在屏幕底部显示三个按钮而不更改为相对布局。 I tried setting the layout_weight and gravity of the inner linear layout but this puts the buttons to the bottom, behind the three menu buttons of the phone. 我尝试设置内部线性布局的layout_weightgravity ,但这会将按钮放在手机三个菜单按钮后面的底部。 How can I show them above the menu buttons? 如何在菜单按钮上方显示它们? Any help would be appreciated. 任何帮助,将不胜感激。

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

Left image shows the buttons I want to move to the bottom. 左图显示了我想要移动到底部的按钮。 Right image shows my attempt to do so but the buttons end up partially behind the system menu buttons. 右图显示我尝试这样做但按钮最终部分位于系统菜单按钮后面。

按钮我想移到底部 电话菜单按钮后面的按钮

Try adding a margin on the bottom of each of your buttons. 尝试在每个按钮的底部添加边距。 Add this to each button layout: android:layout_marginBottom="20dp" 将其添加到每个按钮布局:android:layout_marginBottom =“20dp”

If that doesn't work, add a bottom margin to the linearlayout itself. 如果这不起作用,请向线性布局本身添加底部边距。

set TextView height = 0, weight = 1. And LinearLayout height = wrap content. 设置TextView height = 0,weight = 1.并且LinearLayout height = wrap content。 It will be OK. 一切都会安好的。 Try ! 试试吧!

Try it 试试吧

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

暂无
暂无

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

相关问题 将线性布局中的按钮与屏幕底部对齐 - Align Buttons in Linear Layout to the Bottom of the Screen 如何将线性布局中的内容设置为Android中屏幕的BOTTOM。 - How to set content in a Linear layout to the BOTTOM of the Screen in Android.? 如何在相对布局中包含线性布局的页面底部设置按钮的宽度的一半? - how to set buttons in bottom with half width of page for a linear layout enclosed in relative layout? 如何在顶部和底部设置边框线性布局 - how to set border linear layout on top and bottom 如何将相对布局放置在屏幕底部(或线性布局)。 - How to place Relative layout at bottom of screen(or linear layout).? 如果有2个按钮(其中1个具有特殊宽度),如何设置按钮的宽度以固定线性布局中的其余屏幕尺寸 - How to set a Button's width to fix the remaining screen size inside a Linear Layout if there are 2 Buttons (1 with spcified width) 如何并排设置两个或更多按钮以填充布局底部的屏幕宽度(不像粘性菜单)? - How to set two or more buttons side by side to fill width of screen at bottom of the layout (not like a sticky menu)? 如何在ScrollView内强制线性布局到屏幕底部? - How to force a Linear layout to the bottom of the screen while it's inside a ScrollView? 如何设置表格布局底部到屏幕中央? - How to set Table layout bottom to center of screen? 如何在android中以编程方式为线性布局设置底部边框 - How to set border at bottom for linear layout programactically in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM