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

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

posted @ 2015-08-20 11:42  统领七界  阅读(5463)  评论(0编辑  收藏  举报