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

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内容如下:

[html] view plaincopy

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6. <TextView android:id="@+id/TextView01"
  7. android:layout_width="fill_parent"
  8. android:layout_height="35dp"
  9. android:text="top"
  10. android:gravity="top"
  11. android:textColor="#ffffff"
  12. android:background="#ff0000"
  13. android:layout_margin="1px"/>
  14. <TextView android:id="@+id/TextView02"
  15. android:layout_width="fill_parent"
  16. android:layout_height="35dp"
  17. android:text="bottom"
  18. android:gravity="bottom"
  19. android:textColor="#ffffff"
  20. android:background="#ff0000"
  21. android:layout_margin="1px"/>
  22. <TextView android:id="@+id/TextView03"
  23. android:layout_width="fill_parent"
  24. android:layout_height="35dp"
  25. android:text="left"
  26. android:gravity="left"
  27. android:textColor="#ffffff"
  28. android:background="#ff0000"
  29. android:layout_margin="1px"/>
  30. <TextView android:id="@+id/TextView04"
  31. android:layout_width="fill_parent"
  32. android:layout_height="35dp"
  33. android:text="right"
  34. android:gravity="right"
  35. android:textColor="#ffffff"
  36. android:background="#ff0000"
  37. android:layout_margin="1px"/>
  38. <TextView android:id="@+id/TextView05"
  39. android:layout_width="fill_parent"
  40. android:layout_height="35dp"
  41. android:text="center_vertical"
  42. android:gravity="center_vertical"
  43. android:textColor="#ffffff"
  44. android:background="#ff0000"
  45. android:layout_margin="1px"/>
  46. <TextView android:id="@+id/TextView06"
  47. android:layout_width="fill_parent"
  48. android:layout_height="35dp"
  49. android:text="fill_vertical"
  50. android:gravity="fill_vertical"
  51. android:textColor="#ffffff"
  52. android:background="#ff0000"
  53. android:layout_margin="1px"/>
  54. <TextView android:id="@+id/TextView07"
  55. android:layout_width="fill_parent"
  56. android:layout_height="35dp"
  57. android:text="center_horizontal"
  58. android:gravity="center_horizontal"
  59. android:textColor="#ffffff"
  60. android:background="#ff0000"
  61. android:layout_margin="1px"/>
  62. <TextView android:id="@+id/TextView08"
  63. android:layout_width="fill_parent"
  64. android:layout_height="35dp"
  65. android:text="fill_horizontal"
  66. android:gravity="fill_horizontal"
  67. android:textColor="#ffffff"
  68. android:background="#ff0000"
  69. android:layout_margin="1px"/>
  70. <TextView android:id="@+id/TextView09"
  71. android:layout_width="fill_parent"
  72. android:layout_height="35dp"
  73. android:text="center"
  74. android:gravity="center"
  75. android:textColor="#ffffff"
  76. android:background="#ff0000"
  77. android:layout_margin="1px"/>
  78. <TextView android:id="@+id/TextView10"
  79. android:layout_width="fill_parent"
  80. android:layout_height="35dp"
  81. android:text="fill"
  82. android:gravity="fill"
  83. android:textColor="#ffffff"
  84. android:background="#ff0000"
  85. android:layout_margin="1px"/>
  86. <TextView android:id="@+id/TextView11"
  87. android:layout_width="fill_parent"
  88. android:layout_height="35dp"
  89. android:text="clip_vertical"
  90. android:gravity="clip_vertical"
  91. android:textColor="#ffffff"
  92. android:background="#ff0000"
  93. android:layout_margin="1px"/>
  94. <TextView android:id="@+id/TextView12"
  95. android:layout_width="fill_parent"
  96. android:layout_height="35dp"
  97. android:text="clip_horizontal"
  98. android:gravity="clip_horizontal"
  99. android:textColor="#ffffff"
  100. android:background="#ff0000"
  101. android:layout_margin="1px"/>
  102. </LinearLayout>

效果图2:

xml布局文件如下:

[html] view plaincopy

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6. <TextView android:id="@+id/TextView01"
  7. android:layout_width="fill_parent"
  8. android:layout_height="50dp"
  9. android:text="top"
  10. android:gravity="top"
  11. android:textColor="#ffffff"
  12. android:background="#00ff00"
  13. android:layout_margin="2px"/>
  14. <TextView android:id="@+id/TextView02"
  15. android:layout_width="fill_parent"
  16. android:layout_height="50dp"
  17. android:text="bottom"
  18. android:gravity="bottom"
  19. android:textColor="#ffffff"
  20. android:background="#00ff00"
  21. android:layout_margin="2px"/>
  22. <TextView android:id="@+id/TextView03"
  23. android:layout_width="fill_parent"
  24. android:layout_height="50dp"
  25. android:text="left"
  26. android:gravity="left"
  27. android:textColor="#ffffff"
  28. android:background="#00ff00"
  29. android:layout_margin="2px"/>
  30. <TextView android:id="@+id/TextView04"
  31. android:layout_width="fill_parent"
  32. android:layout_height="50dp"
  33. android:text="right"
  34. android:gravity="right"
  35. android:textColor="#ffffff"
  36. android:background="#00ff00"
  37. android:layout_margin="2px"/>
  38. <TextView android:id="@+id/TextView05"
  39. android:layout_width="fill_parent"
  40. android:layout_height="50dp"
  41. android:text="center_vertical"
  42. android:gravity="center_vertical"
  43. android:textColor="#ffffff"
  44. android:background="#00ff00"
  45. android:layout_margin="2px"/>
  46. <TextView android:id="@+id/TextView06"
  47. android:layout_width="fill_parent"
  48. android:layout_height="50dp"
  49. android:text="fill_vertical"
  50. android:gravity="fill_vertical"
  51. android:textColor="#ffffff"
  52. android:background="#00ff00"
  53. android:layout_margin="2px"/>
  54. <TextView android:id="@+id/TextView07"
  55. android:layout_width="fill_parent"
  56. android:layout_height="50dp"
  57. android:text="center_horizontal"
  58. android:gravity="center_horizontal"
  59. android:textColor="#ffffff"
  60. android:background="#00ff00"
  61. android:layout_margin="2px"/>
  62. <TextView android:id="@+id/TextView08"
  63. android:layout_width="fill_parent"
  64. android:layout_height="50dp"
  65. android:text="fill_horizontal"
  66. android:gravity="fill_horizontal"
  67. android:textColor="#ffffff"
  68. android:background="#00ff00"
  69. android:layout_margin="2px"/>
  70. <TextView android:id="@+id/TextView09"
  71. android:layout_width="fill_parent"
  72. android:layout_height="50dp"
  73. android:text="center"
  74. android:gravity="center"
  75. android:textColor="#ffffff"
  76. android:background="#00ff00"
  77. android:layout_margin="2px"/>
  78. <TextView android:id="@+id/TextView10"
  79. android:layout_width="fill_parent"
  80. android:layout_height="50dp"
  81. android:text="fill"
  82. android:gravity="fill"
  83. android:textColor="#ffffff"
  84. android:background="#00ff00"
  85. android:layout_margin="2px"/>
  86. <TextView android:id="@+id/TextView11"
  87. android:layout_width="fill_parent"
  88. android:layout_height="50dp"
  89. android:text="clip_vertical"
  90. android:gravity="clip_vertical"
  91. android:textColor="#ffffff"
  92. android:background="#00ff00"
  93. android:layout_margin="2px"/>
  94. <TextView android:id="@+id/TextView12"
  95. android:layout_width="fill_parent"
  96. android:layout_height="50dp"
  97. android:text="clip_horizontal"
  98. android:gravity="clip_horizontal"
  99. android:textColor="#ffffff"
  100. android:background="#00ff00"
  101. android:layout_margin="2px"/>
  102. </LinearLayout>

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-08 16:42:19

Android android:gravity属性介绍及效果图的相关文章

Android属性 gravity, layout_gravity, padding, layout_margin 及 关于gravity/layout_gravity的一个Demo

1. android:gravity 对内的相对位置-----用来设置该view的内容在该View内的相对位置 (靠左,靠右,靠上, 靠下, 居中...,不是具体数值). (官方的解释是: gravity---Specifies how to place the content of an object, both on the x- and y-axis, within the object itself. 数值center的含义---Place the object in the cente

Android TextView常用属性介绍Android:drawableEnd

//TextView所呈现的文字 android:text="我爱Java" //文字颜色 android:textColor="#f00" //文字尺寸 android:textSize="20pt" //文本框结尾处绘制图片 android:drawableEnd="@drawable/ic_launcher" //不管内容多长,单行显示 android:singleLine="true" //文字过长

二、 Android中gravity与layout_gravity的区别

android:gravity:设置的是控件自身上面的内容位置 android:layout_gravity:设置控件本身相对于父控件的显示位置. 看下如下代码段 <?xml version="1.0" encoding="utf-8"?><!-- android:gravity设置了按钮上面的文字的显示位置,而android:layout_gravity设置了按钮在布局中的显示位置.--><LinearLayout xmlns:and

Android中gravity与layout_gravity的区别

android里有很多用途相似但效果截然不同的属性,如LinearLayout有两个非常相似的属性:android:gravity与android:layout_gravity.他们的区别在于:android:gravity用于设置View组件的对齐方式,而android:layout_gravity用于设置Container组件的对齐方式. 举个例子, 我们可以通过设置android:gravity="center"来让EditText中的文字在EditText组件中居中显示: 同时

Android android.support.v4.widget.SlidingPaneLayout 侧滑示例

SlidingPaneLayout 用于水平滚动两个view, 第一个view是左侧边,第二个view是content view slding_pane_layout.xml <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.SlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res

Android] Android XML解析学习——方式比较

[Android] Android XML解析学习——方式比较 (ZT) 分类: 嵌入式 (From:http://blog.csdn.net/ichliebephone/article/details/5981913) 一.基础知识 经过这段时间的学习,我们已经了解了Android平台上用于解析XML的三种方式:SAX.DOM和Pull.并且在学习的过程中也介绍了这三种方式各自的特点及适合的使用场合,简单的来说,DOM方式最直观和容易理解,但是只适合XML文档较小的时候使用,而SAX方式更适合

[Android] Android 让UI控件固定于底部的几种方法

Android 让UI控件固定于底部的几种方法1.采用linearlayout布局:android:layout_height="0dp" <!-- 这里不能设置fill_parent -->android:layout_weight="1" <!-- 这里设置layout_weight=1是最关键的,否则底部的LinearLayout无法到底部 --> 2. 采用relativelayout布局:android:layout_alignPa

[Android] Android ViewPager 中加载 Fragment的两种方式 方式(二)

接上文: https://www.cnblogs.com/wukong1688/p/10693338.html Android ViewPager 中加载 Fragmenet的两种方式 方式(一) 二.使用Fragment来填充ViewPager Google 官方是建议我们使用Fragment来填充ViewPager的,这样可以更加方便的生成每个Page以及管理 每个Page的生命周期!当然它给我们提供了两个不同的Adapter,他们分别是: FragmentPageAdapter和Fragm

[Android] Android 支持下拉刷新、上拉加载更多 的 XRecyclerview

XRecyclerView一个实现了下拉刷新,滚动到底部加载更多以及添加header功能的的RecyclerView.使用方式和RecyclerView完全一致,不需要额外的layout,不需要写特殊的adater. 加载效果内置了AVLoadingIndicatorView上的所有效果,可以根据需要指定. 效果演示如下: 插件官网地址: https://github.com/XRecyclerView/XRecyclerView 一.添加依赖 compile 'com.jcodecraeer: