转自: http://blog.csdn.net/aminfo/article/details/7784229

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内容如下:
 
  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布局文件如下:
  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>
 
 

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

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

     [Android] Android XML解析学习——方式比较 (ZT)  分类: 嵌入式 (From:http://blog.csdn.net/ichliebephone/article/deta ...

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

    XRecyclerView一个实现了下拉刷新,滚动到底部加载更多以及添加header功能的的RecyclerView.使用方式和RecyclerView完全一致,不需要额外的layout,不需要写特殊 ...

  3. [Android] Android 使用 FragmentTabHost + Fragment 实现 微信 底部菜单

    Android 使用 FragmentTabHost + Fragment 实现 微信 底部菜单 利用FragmentTabHost实现底部菜单,在该底部菜单中,包括了4个TabSpec,每个TabS ...

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

    接上文: https://www.cnblogs.com/wukong1688/p/10693338.html Android ViewPager 中加载 Fragmenet的两种方式 方式(一) 二 ...

  5. [Android] Android ViewPager 中加载 Fragment的两种方式 方式(一)

    Android ViewPager 中加载 Fragmenet的两种方式 一.当fragment里面的内容较少时,直接 使用fragment xml布局文件填充 文件总数 布局文件:view_one. ...

  6. [Android] Android 卡片式控件CardView的优雅使用

    [Android] Android 卡片式控件CardView的优雅使用 CardView是在安卓5.0提出的卡片式控件 其具体用法如下: 1.在app/build.gradle 文件中添加 comp ...

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

    Android 让UI控件固定于底部的几种方法1.采用linearlayout布局:android:layout_height="0dp" <!-- 这里不能设置fill_p ...

  8. [Android] Android 异步定时任务实现的三种方法(以SeekBar的进度自动实现为例)

    [Android] Android 定时异步任务实现的三种方法(以SeekBar的进度自动实现为例) 一.采用Handler与线程的sleep(long)方法 二.采用Handler与timer及Ti ...

  9. Android <Android应用开发实战> 资源类型<一>

    1.字符串资源>>1.普通字符串>>2.字符串数组 <resources> <string-array name="planets_array&qu ...

随机推荐

  1. 使用 Intel HAXM 为 Android 模拟器加速,媲美真机

    http://www.cnblogs.com/beginor/archive/2013/01/13/2858228.html

  2. ACM - a + b Problem

    前几天看了ACM的第一题,映入眼帘的是一个“简单”的题目: 输入两个数,a,b 输出他们的和. 本着,此乃ACM的原则,便有了如下的思考: ACM的题目肯定很难,a+b,怎么可能直接printf,不行 ...

  3. Ngrok搭建服务器

    一.ngrok简介及作用 ngrok 是一款用go语言开发的开源软件,它是一个反向代理,通过在公共的端点和本地运行的 Web 服务器之间建立一个安全的通道.下图简述了ngrok的原理. ngrok 可 ...

  4. UICollectionView + AFNetWorking 异步加载,局部刷新.

    最近在做的项目需要cell里面的数据需要和后台实时交互.而collectionview reload会整体刷新. //m层 发送通知 [[NSNotificationCenter defaultCen ...

  5. zabbix之MySQL数据库的安装

    转载自:http://www.ttlsa.com/mysql/install-mysql5_6/ 启动MySQL并制定套接字 mysql -u user -ppassword -S 指定socket路 ...

  6. Android SDK在线更新镜像服务器

    http://www.cnblogs.com/tc310/p/4696731.html#_label0

  7. C#之显示效果

    窗体最大化(包含任务栏): this.TopMost = true; , ); this.Size = Screen.PrimaryScreen.Bounds.Size; 窗体最大化(不包含任务栏): ...

  8. POJ 1190(深搜)

    http://poj.org/problem?id=1190 又有好久没做搜索的题了,没想到做一个卡了我那么久,想哭啊. 一个中文题,思路呢也就是搜索呗,一层一层往上面搜,不过这里有两个比较重要的地方 ...

  9. CentOS7下安装soaplib

    测试安装 cd ENV . bin/activate yum install libxml2-devel libxslt-devel pip install soaplib 安装中遇到的问题: Uni ...

  10. 使用Java的多线程和IO流写一个文件复制功能类

    创建一个复制功能类,继承Thread类,重写run()方法,把FileInputStream和FileOutputStream输入输出流写在run()方法内.示例代码如下: import java.i ...