我有以下代码,如何使 3 个按钮位于底部?
<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>
原文由 thedeepfield 发布,翻译遵循 CC BY-SA 4.0 许可协议
你需要确保四件事:
LinearLayout
有layout_height="match_parent"
LinearLayout
有layout_weight="1"
和layout_height="0dp"
TextView
有layout_weight="0"
LinearLayout: android:gravity="center|bottom"
注意
fill_parent
并不意味着“占用所有可用空间”。但是,如果您将layout_height="0dp"
与layout_weight="1"
--- 一起使用,则视图将占用所有可用空间( 无法使用“fill_parent”获得正确的布局)。这是我快速编写的一些代码,它们以与您的代码类似的方式使用两个 LinearLayouts。
结果类似于: