转自: 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. 160809225-叶桦汀《C语言程序设计》实验报告

    #include<stdio.h> int main() { int a,b,c,t; printf("请输入三个整数"); scanf("%d%d%d&qu ...

  2. POJ 1273 网络流(最大流)模板

    http://poj.org/problem?id=1273 这道题很值得反思,弄了一下午,交上去先是一直编译错误,而在本地运行没有问题, 原因可能是oj的编译器版本老旧不支持这样的写法 G[from ...

  3. 9.2---机器人走方格(CC150)

    这题就是dp的一般题目 public static int countWays(int x, int y){ if( x < 0 || y < 0) return -1; int[][] ...

  4. linux下的shell运算(加、减、乘、除)

    linux下的shell运算(加.减.乘.除) 标签: linuxshell运算加减乘除 2014-03-12 16:25 15127人阅读 评论(0) 收藏 举报 分类: linux(17) ((i ...

  5. CLR via C# 随记

    使用C# 编译器的方法: 1.csc.exe位于C:\Windows\Microsoft.NET\Framework\vxxxxx下面,将对应版本的路径配置到环境变量path中,如将";C: ...

  6. SQL Server 中几种常见的约束关系

    1.创建唯一约束 当表中已创建主键,但又要保证其他数据列的值唯一时,可以使用唯一约束,并且唯一约束允许NULL值(只有一个) (1)展开指定的数据库: (2)右击要创建唯一约束的表,在弹出的快捷菜单中 ...

  7. Python之多线程

    廖雪峰教程--- http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/00138683 ...

  8. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  9. Effective C++ -----条款22:将成员变量声明为private

    切记将成员变量声明为private.这可赋予客户访问数据的一致性.可细微划分访问控制.允诺约束条件获得保证,并提供class作者以充分的实现弹性. protected并不比public更具有封装性.

  10. codeforces 556C. Case of Matryoshkas 解题报告

    题目链接:http://codeforces.com/contest/556/problem/C 题目意思:有 n 个数(1,2,...,n)组成 k 条链.第 i 条链由 mi 个数组成.每一秒只可 ...