温馨提示×

Android如何把button上下排列

小亿
175
2023-08-08 22:38:33
栏目: 编程语言

要在Android中将按钮(Button)上下排列,可以使用垂直线性布局(LinearLayout)并设置其方向为垂直(vertical)。

可以按照以下步骤进行操作:

  1. 在XML布局文件中,创建一个垂直线性布局,设置其方向为垂直:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- 添加按钮控件 -->
</LinearLayout>
  1. 在垂直线性布局中添加按钮控件,可以使用多个Button标签,并设置它们的宽度和高度等属性。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 2" />
<!-- 添加更多按钮控件 -->
</LinearLayout>
  1. 最后,将LinearLayout作为根布局或嵌套在其他布局中。

这样设置后,按钮将按照垂直方向依次排列显示。

0