Android android:gravity属性介绍及效果图

本文详细介绍了Android中android:gravity属性的使用,包括CENTER、LEFT、RIGHT、TOP、BOTTOM等常量,以及它们在布局中的作用,帮助开发者更好地控制视图在容器中的位置。

android:gravity的属性官方说明如下:

public static final int AXIS_CLIP
Since: API Level 3
Raw bit controlling whether the right/bottom edge is clipped to its container, based on the gravity direction being applied.
Constant Value: 8 (0x00000008)
public static final int AXIS_PULL_AFTER
Since: API Level 1
Raw bit controlling how the right/bottom edge is placed.
Constant Value: 4 (0x00000004)
public static final int AXIS_PULL_BEFORE
Since: API Level 1
Raw bit controlling how the left/top edge is placed.
Constant Value: 2 (0x00000002)
public static final int AXIS_SPECIFIED
Since: API Level 1
Raw bit indicating the gravity for an axis has been specified.
Constant Value: 1 (0x00000001)
public static final int AXIS_X_SHIFT
Since: API Level 1
Bits defining the horizontal axis.
Constant Value: 0 (0x00000000)
public static final int AXIS_Y_SHIFT
Since: API Level 1
Bits defining the vertical axis.
Constant Value: 4 (0x00000004)
public static final int BOTTOM
Since: API Level 1
Push object to the bottom of its container, not changing its size.
Constant Value: 80 (0x00000050)
public static final int CENTER
Since: API Level 1
Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.
Constant Value: 17 (0x00000011)
public static final int CENTER_HORIZONTAL
Since: API Level 1
Place object in the horizontal center of its container, not changing its size.
Constant Value: 1 (0x00000001)
public static final int CENTER_VERTICAL
Since: API Level 1
Place object in the vertical center of its container, not changing its size.
Constant Value: 16 (0x00000010)
public static final int CLIP_HORIZONTAL
Since: API Level 3
Flag to clip the edges of the object to its container along the horizontal axis.
Constant Value: 8 (0x00000008)
public static final int CLIP_VERTICAL
Since: API Level 3
Flag to clip the edges of the object to its container along the vertical axis.
Constant Value: 128 (0x00000080)
public static final int DISPLAY_CLIP_HORIZONTAL
Since: API Level 3
Special constant to enable clipping to an overall display along the horizontal dimension. This is not applied by default by apply(int, int, int, Rect, int, int, Rect); you must do so yourself by calling applyDisplay(int, Rect, Rect).
Constant Value: 16777216 (0x01000000)
public static final int DISPLAY_CLIP_VERTICAL
Since: API Level 3
Special constant to enable clipping to an overall display along the vertical dimension. This is not applied by default by apply(int, int, int, Rect, int, int, Rect); you must do so yourself by calling applyDisplay(int, Rect, Rect).
Constant Value: 268435456 (0x10000000)
public static final int END
Since: API Level 14
Push object to x-axis position at the end of its container, not changing its size.
Constant Value: 8388613 (0x00800005)
public static final int FILL
Since: API Level 1
Grow the horizontal and vertical size of the object if needed so it completely fills its container.
Constant Value: 119 (0x00000077)
public static final int FILL_HORIZONTAL
Since: API Level 1
Grow the horizontal size of the object if needed so it completely fills its container.
Constant Value: 7 (0x00000007)
public static final int FILL_VERTICAL
Since: API Level 1
Grow the vertical size of the object if needed so it completely fills its container.
Constant Value: 112 (0x00000070)
public static final int HORIZONTAL_GRAVITY_MASK
Since: API Level 1
Binary mask to get the absolute horizontal gravity of a gravity.
Constant Value: 7 (0x00000007)
public static final int LEFT
Since: API Level 1
Push object to the left of its container, not changing its size.
Constant Value: 3 (0x00000003)
public static final int NO_GRAVITY
Since: API Level 1
Constant indicating that no gravity has been set
Constant Value: 0 (0x00000000)
public static final int RELATIVE_HORIZONTAL_GRAVITY_MASK
Since: API Level 14
Binary mask for the horizontal gravity and script specific direction bit.
Constant Value: 8388615 (0x00800007)
public static final int RELATIVE_LAYOUT_DIRECTION
Since: API Level 14
Raw bit controlling whether the layout direction is relative or not (START/END instead of absolute LEFT/RIGHT).
Constant Value: 8388608 (0x00800000)
public static final int RIGHT
Since: API Level 1
Push object to the right of its container, not changing its size.
Constant Value: 5 (0x00000005)
public static final int START
Since: API Level 14
Push object to x-axis position at the start of its container, not changing its size.
Constant Value: 8388611 (0x00800003)
public static final int TOP
Since: API Level 1
Push object to the top of its container, not changing its size.
Constant Value: 48 (0x00000030)
public static final int VERTICAL_GRAVITY_MASK
Since: API Level 1
Binary mask to get the vertical gravity of a gravity.
Constant Value: 112 (0x00000070)
 
效果图1:
 
布局文件xml内容如下:
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical">
   
   <TextView android:id="@+id/TextView01"
   		android:layout_width="fill_parent"
   		android:layout_height="35dp"
   		android:text="top"
   		android:gravity="top"
   		android:textColor="#ffffff"
   		android:background="#ff0000"
   		android:layout_margin="1px"/>
   		
   <TextView android:id="@+id/TextView02"
   		android:layout_width="fill_parent"
   		android:layout_height="35dp"
   		android:text="bottom"
   		android:gravity="bottom"
   		android:textColor="#ffffff"
   		android:background="#ff0000"
   		android:layout_margin="1px"/>
   		
   <TextView android:id="@+id/TextView03"
   		android:layout_width="fill_parent"
   		android:layout_height="35dp"
   		android:text="left"
   		android:gravity="left"
   		android:textColor="#ffffff"
   		android:background="#ff0000"
   		android:layout_margin="1px"/>
   		
   <TextView android:id="@+id/TextView04"
   		android:layout_width="fill_parent"
   		android:layout_height="35dp"
   		android:text="right"
   		android:gravity="right"
   		android:textColor="#ffffff"
   		android:background="#ff0000"
   		android:layout_margin="1px"/>
   		
   <TextView android:id="@+id/TextView05"
   		android:layout_width="fill_parent"
   		android:layout_height="35dp"
   		android:text="center_vertical"
   		android:gravity="center_vertical"
   		android:textColor="#ffffff"
   		android:background="#ff0000"
   		android:layout_margin="1px"/>
   		
   <TextView android:id="@+id/TextView06"
   		android:layout_width="fill_parent"
   		android:layout_height="35dp"
   		android:text="fill_vertical"
   		android:gravity="fill_vertical"
   		android:textColor="#ffffff"
   		android:background="#ff0000"
   		android:layout_margin="1px"/>
   		
   <TextView android:id="@+id/TextView07"
   		android:layout_width="fill_parent"
   		android:layout_height="35dp"
   		android:text="center_horizontal"
   		android:gravity="center_horizontal"
   		android:textColor="#ffffff"
   		android:background="#ff0000"
   		android:layout_margin="1px"/>
   		
   <TextView android:id="@+id/TextView08"
   		android:layout_width="fill_parent"
   		android:layout_height="35dp"
   		android:text="fill_horizontal"
   		android:gravity="fill_horizontal"
   		android:textColor="#ffffff"
   		android:background="#ff0000"
   		android:layout_margin="1px"/>   		   		   		   		   		   		   		

   <TextView android:id="@+id/TextView09"
   		android:layout_width="fill_parent"
   		android:layout_height="35dp"
   		android:text="center"
   		android:gravity="center"
   		android:textColor="#ffffff"
   		android:background="#ff0000"
   		android:layout_margin="1px"/>
   		
   <TextView android:id="@+id/TextView10"
   		android:layout_width="fill_parent"
   		android:layout_height="35dp"
   		android:text="fill"
   		android:gravity="fill"
   		android:textColor="#ffffff"
   		android:background="#ff0000"
   		android:layout_margin="1px"/>
   		
   <TextView android:id="@+id/TextView11"
   		android:layout_width="fill_parent"
   		android:layout_height="35dp"
   		android:text="clip_vertical"
   		android:gravity="clip_vertical"
   		android:textColor="#ffffff"
   		android:background="#ff0000"
   		android:layout_margin="1px"/>
   		
   <TextView android:id="@+id/TextView12"
   		android:layout_width="fill_parent"
   		android:layout_height="35dp"
   		android:text="clip_horizontal"
   		android:gravity="clip_horizontal"
   		android:textColor="#ffffff"
   		android:background="#ff0000"
   		android:layout_margin="1px"/>
   		   		   		   		   		
</LinearLayout>
 
效果图2:
 
xml布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical">
   
   <TextView android:id="@+id/TextView01"
   		android:layout_width="fill_parent"
   		android:layout_height="50dp"
   		android:text="top"
   		android:gravity="top"
   		android:textColor="#ffffff"
   		android:background="#00ff00"
   		android:layout_margin="2px"/>
   		
   <TextView android:id="@+id/TextView02"
   		android:layout_width="fill_parent"
   		android:layout_height="50dp"
   		android:text="bottom"
   		android:gravity="bottom"
   		android:textColor="#ffffff"
   		android:background="#00ff00"
   		android:layout_margin="2px"/>
   		
   <TextView android:id="@+id/TextView03"
   		android:layout_width="fill_parent"
   		android:layout_height="50dp"
   		android:text="left"
   		android:gravity="left"
   		android:textColor="#ffffff"
   		android:background="#00ff00"
   		android:layout_margin="2px"/>
   		
   <TextView android:id="@+id/TextView04"
   		android:layout_width="fill_parent"
   		android:layout_height="50dp"
   		android:text="right"
   		android:gravity="right"
   		android:textColor="#ffffff"
   		android:background="#00ff00"
   		android:layout_margin="2px"/>
   		
   <TextView android:id="@+id/TextView05"
   		android:layout_width="fill_parent"
   		android:layout_height="50dp"
   		android:text="center_vertical"
   		android:gravity="center_vertical"
   		android:textColor="#ffffff"
   		android:background="#00ff00"
   		android:layout_margin="2px"/>
   		
   <TextView android:id="@+id/TextView06"
   		android:layout_width="fill_parent"
   		android:layout_height="50dp"
   		android:text="fill_vertical"
   		android:gravity="fill_vertical"
   		android:textColor="#ffffff"
   		android:background="#00ff00"
   		android:layout_margin="2px"/>
   		
   <TextView android:id="@+id/TextView07"
   		android:layout_width="fill_parent"
   		android:layout_height="50dp"
   		android:text="center_horizontal"
   		android:gravity="center_horizontal"
   		android:textColor="#ffffff"
   		android:background="#00ff00"
   		android:layout_margin="2px"/>
   		
   <TextView android:id="@+id/TextView08"
   		android:layout_width="fill_parent"
   		android:layout_height="50dp"
   		android:text="fill_horizontal"
   		android:gravity="fill_horizontal"
   		android:textColor="#ffffff"
   		android:background="#00ff00"
   		android:layout_margin="2px"/>   		   		   		   		   		   		   		

   <TextView android:id="@+id/TextView09"
   		android:layout_width="fill_parent"
   		android:layout_height="50dp"
   		android:text="center"
   		android:gravity="center"
   		android:textColor="#ffffff"
   		android:background="#00ff00"
   		android:layout_margin="2px"/>
   		
   <TextView android:id="@+id/TextView10"
   		android:layout_width="fill_parent"
   		android:layout_height="50dp"
   		android:text="fill"
   		android:gravity="fill"
   		android:textColor="#ffffff"
   		android:background="#00ff00"
   		android:layout_margin="2px"/>
   		
   <TextView android:id="@+id/TextView11"
   		android:layout_width="fill_parent"
   		android:layout_height="50dp"
   		android:text="clip_vertical"
   		android:gravity="clip_vertical"
   		android:textColor="#ffffff"
   		android:background="#00ff00"
   		android:layout_margin="2px"/>
   		
   <TextView android:id="@+id/TextView12"
   		android:layout_width="fill_parent"
   		android:layout_height="50dp"
   		android:text="clip_horizontal"
   		android:gravity="clip_horizontal"
   		android:textColor="#ffffff"
   		android:background="#00ff00"
   		android:layout_margin="2px"/>
   		   		   		   		   		
</LinearLayout>


<think>我们正在讨论的是Android中的`android:gravity`属性,特别是它在按钮(Button)中的作用,以及`android:gravity="center"`的具体含义。 根据引用[1]和引用[2]: - `android:gravity`用于设置视图(View)内部内容的对齐方式。 - 对于按钮(Button)来说,它内部的内容主要是文本(Text)或者图标(Icon)。 - 因此,`android:gravity`属性在按钮中用于控制按钮内文本(或图标)的对齐方式。 具体到`android:gravity="center"`: - 这个属性值表示将按钮内部的文本(或图标)在水平和垂直方向上都居中显示。 - 也就是说,文本会显示在按钮的正中央。 引用[3]中展示了在代码中设置`gravity`的方法: - 在Java代码中,可以通过`setGravity(int gravity)`方法来设置,例如:`button.setGravity(Gravity.CENTER);` 注意:`android:gravity`和`android:layout_gravity`的区别(引用[1]和引用[2]): - `android:gravity`控制的是视图内部内容的对齐。 - `android:layout_gravity`控制的是视图本身在其父容器中的对齐方式。 因此,对于按钮,如果我们希望按钮内的文字居中,则设置`android:gravity="center"`。如果我们希望整个按钮在其父布局(比如LinearLayout)中居中,则设置`android:layout_gravity="center"`。 示例代码: ```xml <Button android:id="@+id/my_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click me" android:gravity="center" /> <!-- 设置按钮内部文本居中 --> ``` 在这个例子中,按钮的文本会在按钮内部居中显示。即使按钮的宽度和高度发生变化,文本也会始终保持在按钮中央。 总结: - `android:gravity="center"`在按钮中的作用:让按钮内的文本(或图标)在按钮内部居中显示。 相关问题: 1. 如何同时设置按钮内文本的水平居中和垂直居中?(答案:使用`center`) 2. 如果只想水平居中或垂直居中,应该设置什么值? 3. 在LinearLayout中,如何让按钮本身居中显示?</think>在 Android 开发中,`android:gravity` 属性用于**控制视图内部内容(如文本、图标等)的对齐方式**。对于按钮(Button)而言,该属性专门用于控制按钮文本在按钮区域内的位置布局。 ### `android:gravity="center"` 在按钮中的作用 当您为按钮设置 `android:gravity="center"` 属性时,会产生以下效果: 1. **水平居中** 按钮文本会在按钮宽度范围内水平居中显示 ```xml <Button android:text="居中文本" android:gravity="center_horizontal"/> <!-- 仅水平居中 --> ``` 2. **垂直居中** 按钮文本会在按钮高度范围内垂直居中显示 ```xml <Button android:text="居中文本" android:gravity="center_vertical"/> <!-- 仅垂直居中 --> ``` 3. **完全居中(最常用)** `center` 是 `center_horizontal | center_vertical` 的组合值 ```xml <Button android:text="居中文本" android:gravity="center"/> <!-- 水平+垂直居中 --> ``` - 效果:文本在按钮区域的水平和垂直方向都居中显示 - 对比:默认情况下按钮文本是左对齐+垂直居中 ### 实际效果示意图 | 属性设置 | 效果 | |----------|------| | `android:gravity="left"` | ![](https://via.placeholder.com/150x50?text=左对齐文本) | | `android:gravity="center"` | ![](https://via.placeholder.com/150x50?text=居中文本) | | `android:gravity="right"` | ![](https://via.placeholder.com/150x50?text=右对齐文本) | ### 与 `android:layout_gravity` 的区别 | 属性 | 作用对象 | 控制范围 | 示例 | |------|----------|----------|------| | **`android:gravity`** | 视图内部内容 | 控制按钮文本在按钮区域内的位置 | `android:gravity="center"` | | **`android:layout_gravity`** | 视图本身 | 控制整个按钮在其父容器中的位置 | `android:layout_gravity="center"` | ```xml <!-- 示例:按钮文本居中 + 整个按钮在父布局居中 --> <Button android:text="双重居中" android:gravity="center" <!-- 文本在按钮内居中 --> android:layout_gravity="center"/> <!-- 整个按钮在父容器居中 --> ``` ### 在代码中动态设置 通过 Java/Kotlin 动态修改文本对齐方式: ```java // Java 示例 Button btn = findViewById(R.id.my_button); btn.setGravity(Gravity.CENTER); // 设置居中 ``` ```kotlin // Kotlin 示例 findViewById<Button>(R.id.my_button).gravity = Gravity.CENTER ``` ### 其他常用值 | 属性值 | 作用 | 组合值示例 | |--------|------|------------| | `top` | 顶部对齐 | `top|center_horizontal` | | `bottom` | 底部对齐 | `bottom|center_horizontal` | | `start` | 起始边对齐(适配RTL布局) | `start|center_vertical` | | `end` | 结束边对齐(适配RTL布局) | `end|center_vertical` | > **提示**:对于多行文本按钮,使用 `center` 会使所有文本行在按钮区域内整体居中显示[^1][^2]。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值