使用 LinearLayout 将按钮放在屏幕底部? [英] Put buttons at bottom of screen with LinearLayout?
本文介绍了使用 LinearLayout 将按钮放在屏幕底部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有下面的代码,我怎么做才能让 3 个按钮在底部?
I have the following code, how do I make it so that the 3 buttons are at the bottom?
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:gravity="center"
android:text="@string/observer"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:context=".asdf"
android:weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center"
android:text="1" />
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center"
android:text="2" />
<Button
android:id="@+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center"
android:text="3" />
</LinearLayout>
推荐答案
你需要确保四件事:
- 你的外部
LinearLayout
有layout_height="match_parent"
- 你的内部
LinearLayout
有layout_weight="1"
和layout_height="0dp"
- 你的
TextView
有layout_weight="0"
- 您已经在内部
LinearLayout: android:gravity="center|bottom"
上正确设置了重力
- Your outside
LinearLayout
haslayout_height="match_parent"
- Your inside
LinearLayout
haslayout_weight="1"
andlayout_height="0dp"
- Your
TextView
haslayout_weight="0"
- You've set the gravity properly on your inner
LinearLayout: android:gravity="center|bottom"
请注意,fill_parent
并不意味着占用所有可用空间".但是,如果您将 layout_height="0dp"
与 layout_weight="1"
一起使用,则视图将占用所有可用空间(无法使用fill_parent"获得正确的布局).
Notice that fill_parent
does not mean "take up all available space". However, if you use layout_height="0dp"
with layout_weight="1"
, then a view will take up all available space (Can't get proper layout with "fill_parent").
这是我快速编写的一些代码,它们以与您的代码类似的方式使用了两个 LinearLayout.
Here is some code I quickly wrote up that uses two LinearLayouts in a similar fashion to your code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/db1_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/cow"
android:layout_weight="0"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center|bottom"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center"
android:text="1" />
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center"
android:text="2" />
<Button
android:id="@+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center"
android:text="3" />
</LinearLayout>
</LinearLayout>
结果类似于:
这篇关于使用 LinearLayout 将按钮放在屏幕底部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文