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 >

1.首先来看看android:layout_gravity和android:gravity的使用区别。

android:gravity:

这个是针对控件里的元素来说的,用来控制元素在该 控件里的显示位置。例如,在一个Button按钮控件中设置如下两个属性,

android:gravity="left"和android:text="提交",这时Button上的文字“提交”将会位于Button的左部。

android:layout_gravity:

这个是针对控件本身而言,用来控制该控件在包含该控件的父控件中的位置。同样,当我们在 Button按钮控件中设置 android:layout_gravity="left" 属性时,表示该Button按钮将位于界面的左部。

2.属性值:

这两个属性可选的值有:top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical。

一个属性可以包含多个值,需用“|”分开。其含义如下:

top 将对象放在其容器的顶部,不改变其大小.
bottom 将对象放在其容器的底部,不改变其大小.
left 将对象放在其容器的左侧,不改变其大小.
right 将对象放在其容器的右侧,不改变其大小.
center_vertical 将对象纵向居中,不改变其大小.
垂直对齐方式:垂直方向上居中对齐。
fill_vertical 必要的时候增加对象的纵向大小,以完全充满其容器.
垂直方向填充
center_horizontal 将对象横向居中,不改变其大小.
水平对齐方式:水平方向上居中对齐
fill_horizontal 必要的时候增加对象的横向大小,以完全充满其容器.
水平方向填充
center 将对象横纵居中,不改变其大小.
fill 必要的时候增加对象的横纵向大小,以完全充满其容器.
clip_vertical

附加选项,用于按照容器的边来剪切对象的顶部和/或底部的内容. 剪切基于其纵向对齐设置:顶部对齐时,剪切底部;底部对齐时剪切顶部;除此之外剪切顶部和底部.

垂直方向裁剪

clip_horizontal

附加选项,用于按照容器的边来剪切对象的左侧和/或右侧的内容. 剪切基于其横向对齐设置:左侧对齐时,剪切右侧;右侧对齐时剪切左侧;除此之外剪切左侧和右侧.

水平方向裁剪

我们主要来看看 center_vertical和 center_horizontal两个属性值, center_vertical 是指将对象在垂直方向上居中对齐,即 在从上到下的方向上选择中间的位置放好 center_horizontal是指将对象 水平方向上居中对齐,即在 从左到右的方向上选择中间的位置放好

3.特殊情况

当我们采用 LinearLayout布局时,有以下特殊情况需要我们注意:

(1)当 android:orientation="vertical"  时, android:layout_gravity 只有水平方向的设置才起作用,垂直方向的设置不起作用。即:left,right,center_horizontal 是生效的。

(2)当 android:orientation="horizontal" 时, android:layout_gravity 只有垂直方向的设置才起作用,水平方向的设置不起作用。即:top,bottom,center_vertical 是生效的。

下面以一个例子说明:(本例来源于: http://blog.csdn.net/dekunchenivan/article/details/6718678

  1. <? xml version = "1.0" encoding = "utf-8" ?>
  2. < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
  3. android:orientation = "vertical"
  4. android:layout_width = "fill_parent"
  5. android:layout_height = "fill_parent"
  6. >
  7. < TextView
  8. android:layout_width = "100dip"
  9. android:layout_height = "100dip"
  10. android:layout_gravity = "bottom|center_horizontal"
  11. android:gravity = "center|bottom"
  12. android:background = "#00FF00"
  13. android:text = "@string/textview"
  14. />
  15. < Button
  16. android:layout_width = "100dip"
  17. android:layout_height = "100dip"
  18. android:layout_gravity = "bottom|left"
  19. android:gravity = "left|top"
  20. android:background = "#FF0000"
  21. android:text = "@string/button"
  22. />
  23. </ LinearLayout >

其效果如图:

效果图1: 布局文件xml内容如下: [html] view plaincopyxml version="1.0" encoding="utf-8"?>  LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fi 1、 android :layout_ gravity 是设置该控件相对于父容器对齐方式; 2、 android gravity 是设置子元素在该容器内的对齐方式。(若其下没有子元素,此项无效;若父子产生冲突,以子元素为准。) 3、layout_ gravity gravity 可以设置的值:top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical。
今天,简单讲讲 android LinearLayout中如何使用set Gravity ()。 之前,在做一个功能时,需要根据数据的变化设置LinearLayout的 gravity 。之前,自己写代码都是直接在xml文件里通过roid: gravity 设置的 属性 ,这次需要在java代码里动态设置时,居然不知道怎么设置。后来,在网上搜索了资料后,发现set Gravity ()就可以设置,这里记录一下。 在进行UI布局的时候,可能经常会用到 android : gravity android :layout_Gra...