Android线性布局
时间: 2023-10-25 10:23:42 浏览: 45
在Android中,线性布局(LinearLayout)是最常用的布局之一。它可以在水平或垂直方向上排列子视图。
以下是一个简单的示例,演示如何在水平方向上排列两个按钮:
```
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"/>
</LinearLayout>
```
在这个例子中,我们使用`android:orientation="horizontal"`指定了线性布局的方向为水平。`android:layout_width`属性设置为`match_parent`,使布局与父视图的宽度相同。`android:layout_height`属性设置为`wrap_content`,表示布局的高度将根据其内容自适应。
在布局中,我们添加了两个按钮。每个按钮都有一个`android:layout_width`和`android:layout_height`属性,它们分别设置为`wrap_content`,表示按钮的大小将根据其内容自适应。按钮文本使用`android:text`属性设置。
这是一个简单的例子,但它演示了如何使用线性布局在Android应用程序中排列视图。