文章目录
- **1. 常用取值**
- **示例**
- **2. `layout_gravity`(控制 View 在父容器中的对齐方式)**
- **常用取值**
- **示例**
- **3. `gravity` vs `layout_gravity` 对比**
- **4. 注意事项**
- **5. 总结**

作用对象:当前 View 的内部内容(如 TextView 的文本、Button 的文字、LinearLayout 的子控件等)。
适用控件:TextView
、Button
、LinearLayout
、RelativeLayout
等几乎所有 View。
1. 常用取值
值 | 说明 |
---|---|
left / start | 左对齐(start 适配 RTL 布局) |
right / end | 右对齐(end 适配 RTL 布局) |
top | 顶部对齐 |
bottom | 底部对齐 |
center | 水平 + 垂直居中 |
center_horizontal | 水平居中 |
center_vertical | 垂直居中 |
fill / fill_horizontal / fill_vertical | 填充(较少使用) |
示例
<!-- TextView 的文本居中 -->
<TextView
android:layout_width="200dp"
android:layout_height="100dp"
android:text="Hello World"
android:gravity="center" /> <!-- 文本在 TextView 内部居中 -->
<!-- LinearLayout 的子控件垂直居中 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:gravity="center_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. layout_gravity
(控制 View 在父容器中的对齐方式)
作用对象:当前 View 在其父容器 中的位置(如 Button 在 LinearLayout 中的对齐方式)。
适用控件:主要用于 LinearLayout
、FrameLayout
的子控件(RelativeLayout
和 ConstraintLayout
一般不用)。
常用取值
值 | 说明 |
---|---|
left / start | 靠左(start 适配 RTL) |
right / end | 靠右(end 适配 RTL) |
top | 靠顶部 |
bottom | 靠底部 |
center | 居中 |
center_horizontal | 水平居中 |
center_vertical | 垂直居中 |
fill_horizontal | 水平填充(拉伸) |
fill_vertical | 垂直填充(拉伸) |
示例
<!-- FrameLayout 中的 Button 居中 -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="200dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:layout_gravity="center" /> <!-- Button 在 FrameLayout 中居中 -->
</FrameLayout>
<!-- LinearLayout 中的 Button 靠右 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<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"
android:layout_gravity="end" /> <!-- Button 2 靠右 -->
</LinearLayout>
3. gravity
vs layout_gravity
对比
属性 | 作用对象 | 适用场景 | 示例 |
---|---|---|---|
gravity | 当前 View 的内部内容 | TextView 文本居中、LinearLayout 子控件对齐 | android:gravity="center" |
layout_gravity | 当前 View 在其父容器中的位置 | Button 在 FrameLayout 居中、View 在 LinearLayout 靠右 | android:layout_gravity="center" |
4. 注意事项
-
layout_gravity
在LinearLayout
中的限制:- 如果
LinearLayout
是horizontal
方向,layout_gravity
只能控制 垂直方向(top
/bottom
/center_vertical
)。 - 如果
LinearLayout
是vertical
方向,layout_gravity
只能控制 水平方向(left
/right
/center_horizontal
)。
- 如果
-
ConstraintLayout
替代方案:-
ConstraintLayout
一般不使用layout_gravity
,而是用app:layout_constraintXXX_toXXXOf="parent"
控制位置:<Button android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
-
-
gravity
和layout_gravity
可以组合使用:<LinearLayout android:layout_width="match_parent" android:layout_height="200dp" android:gravity="center"> <!-- 子控件整体居中 --> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:layout_gravity="end" /> <!-- 但 Button 自己靠右 --> </LinearLayout>
5. 总结
gravity
→ 控制 当前 View 内部 的对齐(如文本、子控件)。layout_gravity
→ 控制 当前 View 在父容器 中的对齐(如 Button 在 LinearLayout 中的位置)。LinearLayout
方向影响layout_gravity
的有效方向。ConstraintLayout
用约束替代layout_gravity
。
掌握这两个属性可以更灵活地控制 Android 布局! 🚀
在 Android 布局中,layout_weight
是用于 LinearLayout 的一个属性,它允许子视图按照权重(weight)分配剩余空间,常用于实现比例布局(如按比例分配宽度或高度)。