[英]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_weight
和gravity
,但这会将按钮放在手机三个菜单按钮后面的底部。 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.