android系统五大布局实例
时间: 2023-08-10 18:18:10 浏览: 51
好的,下面给出每种布局的一个简单实例:
1. 线性布局(LinearLayout)
```xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello"
android:textSize="20sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="World"
android:textSize="20sp" />
</LinearLayout>
```
2. 相对布局(RelativeLayout)
```xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
android:textSize="20sp" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="World"
android:textSize="20sp"
android:layout_below="@id/text1"
android:layout_alignParentRight="true" />
</RelativeLayout>
```
3. 帧布局(FrameLayout)
```xml
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/image1"
android:scaleType="centerCrop" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
android:textSize="20sp"
android:layout_gravity="center" />
</FrameLayout>
```
4. 表格布局(TableLayout)
```xml
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your name" />
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your age" />
</TableRow>
</TableLayout>
```
5. 网格布局(GridLayout)
```xml
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="3"
android:columnCount="3">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="1"
android:textSize="20sp"
android:layout_row="0"
android:layout_column="0"
android:layout_columnWeight="1" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="2"
android:textSize="20sp"
android:layout_row="0"
android:layout_column="1"
android:layout_columnWeight="1" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="3"
android:textSize="20sp"
android:layout_row="0"
android:layout_column="2"
android:layout_columnWeight="1" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="4"
android:textSize="20sp"
android:layout_row="1"
android:layout_column="0"
android:layout_columnWeight="1" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="5"
android:textSize="20sp"
android:layout_row="1"
android:layout_column="1"
android:layout_columnWeight="1" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="6"
android:textSize="20sp"
android:layout_row="1"
android:layout_column="2"
android:layout_columnWeight="1" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="7"
android:textSize="20sp"
android:layout_row="2"
android:layout_column="0"
android:layout_columnWeight="1" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="8"
android:textSize="20sp"
android:layout_row="2"
android:layout_column="1"
android:layout_columnWeight="1" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="9"
android:textSize="20sp"
android:layout_row="2"
android:layout_column="2"
android:layout_columnWeight="1" />
</GridLayout>
```
以上是五种布局的简单实例,开发者可以根据具体需求进行修改和调整。