1. LinearLayout
LinearLayout 是最简单的布局类型之一,它可以沿一个方向(水平或垂直)排列子视图。
属性:
android:orientation:设置布局的方向,可以是 horizontal 或 vertical。
示例:
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="LinearLayout Example" />
2. RelativeLayout
RelativeLayout 允许子视图相对于父视图或其他子视图的位置进行排列。
属性:
android:layout_alignParentTop、android:layout_centerInParent、android:layout_below 等:用于定义子视图相对位置。
示例
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RelativeLayout Example" android:layout_centerInParent="true" />
3. ConstraintLayout
ConstraintLayout 是一种功能强大的布局类型,允许你创建复杂的布局,且性能更高。它通过约束(Constraints)来定义子视图的位置和大小。
属性:
app:layout_constraintTop_toTopOf、app:layout_constraintBottom_toBottomOf 等:用于定义视图之间的约束关系。
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ConstraintLayout Example" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" />
4. FrameLayout
FrameLayout 是一个简单的布局类型,用于堆叠多个子视图。每个子视图都被放置在框架的左上角。
属性:
没有特别的布局属性,通常用来简单堆叠视图或作为占位符。
示例:
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/example_image" /> android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="FrameLayout Example" android:layout_gravity="center" />
5. TableLayout
TableLayout 用于创建表格布局,由多个 TableRow 组成,每个 TableRow 包含一行元素。
属性:
android:stretchColumns:指定哪一列应该伸展以填充空白空间。
示例:
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Row 1, Column 1" /> android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Row 1, Column 2" /> android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Row 2, Column 1" /> android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Row 2, Column 2" />
6. GridLayout
GridLayout 是一种网格布局,可以将子视图排列在一个网格中,行和列的交叉点处。
属性:
android:rowCount:定义行数。android:columnCount:定义列数。
示例:
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:rowCount="2" android:columnCount="2"> android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Row 1, Column 1" android:layout_row="0" android:layout_column="0" />
总结
LinearLayout:按顺序排列子视图,可以是水平或垂直方向。RelativeLayout:根据相对位置排列子视图。ConstraintLayout:使用约束创建复杂布局,性能较高。FrameLayout:简单堆叠子视图,子视图默认放在左上角。TableLayout:创建表格布局,由多个 TableRow 组成。GridLayout:将子视图排列在网格中,按行和列交叉点排列。