Android 6大布局

在Android开发中,布局是非常重要的部分。Android提供了多种布局方式来帮助开发者构建灵活且美观的界面。其中,Android6大布局是常用的布局方式之一。下面将介绍Android6大布局并附上代码示例。

1. 线性布局(LinearLayout)

线性布局是Android中最简单的布局之一,它按照水平或垂直方向排列子视图。

```xml
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    
    <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>

## 2. 相对布局(RelativeLayout)

相对布局允许子视图相对于另一个视图进行定位。

```markdown
```xml
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1"
        android:layout_alignParentTop="true"/>
        
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2"
        android:layout_below="@id/button1"/>
</RelativeLayout>

## 3. 帧布局(FrameLayout)

帧布局允许子视图叠放在一起,后添加的视图会覆盖先添加的视图。

```markdown
```xml
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image1"/>
        
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image2"/>
</FrameLayout>

## 4. 表格布局(TableLayout)

表格布局将子视图显示为表格行和列的形式。

```markdown
```xml
<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    
    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Row 1, Column 1"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Row 1, Column 2"/>
    </TableRow>
    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Row 2, Column 1"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Row 2, Column 2"/>
    </TableRow>
</TableLayout>

## 5. 网格布局(GridLayout)

网格布局允许子视图以网格的形式排列。

```markdown
```xml
<GridLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnCount="2">
    
    <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"/>
</GridLayout>

## 6. 约束布局(ConstraintLayout)

约束布局是Android中最灵活和强大的布局方式,可以根据视图之间的约束关系来确定位置。

```markdown
```xml
<ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
        
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2"
        app:layout_constraintStart_toEndOf="@id/button1"
        app:layout_constraintTop_toTopOf="parent"/>