RelativeLayout 是 Android 中常用的布局容器,允许子视图通过相对位置进行排列。通过设置 android:layout_above
、android:layout_below
、android:layout_toLeftOf
等属性,可以实现灵活的界面布局。
📌 常用属性示例
相对定位
<Button android:id="@+id/button1" android:layout_above="@+id/textView1" android:layout_centerHorizontal="true" />
对齐方式
android:layout_alignParentTop
(贴靠父容器顶部)android:layout_toRightOf
(右对齐指定视图)android:layout_centerInParent
(居中对齐)
🛠️ 使用技巧
- 避免过度嵌套,保持布局简洁
- 利用
android:layout_margin
设置边距 - 结合
ConstraintLayout
实现更复杂的布局逻辑 - 通过
android:layout_weight
分配空间权重
如需进一步了解线性布局,可访问:Android LinearLayout 教程