布局orientation
时间: 2023-12-02 15:42:58 浏览: 28
布局orientation是指布局的方向,可以设置为水平或垂直。在Android中,LinearLayout和RelativeLayout都支持orientation属性。其中,LinearLayout的默认方向是水平方向,而RelativeLayout没有默认方向。
以下是两个例子:
1.设置LinearLayout的orientation为垂直方向
```xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me!" />
</LinearLayout>
```
2.设置RelativeLayout的orientation为水平方向(无效)
```xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me!" />
</RelativeLayout>
```