grivaty: 内部控件的对齐方式(LinearLayout 、RelativeLayout)
当LinearLayout线性方向为垂直方向时,center表示水平居中,但是并不能垂直居中,此时等同于center_horizontal的作用;
同样当线性方向为水平方向时,center表示垂直居中,等同于center_vertical。
gravity的中文意思就是”重心“,就是表示view横向和纵向的停靠位置
(1).android:gravity:是对view控件本身来说的,是用来设置view本身的内容应该显示在view的什么位置,默认值是左侧。也可以用来设置布局中的控件位置
(2).android:layout_gravity:是相对于包含改元素的父元素来说的,设置该元素在父元素的什么位置;
比如TextView: android:layout_gravity表示TextView在界面上的位置,android:gravity表示TextView文本在TextView的什么位置,默认值是左侧
android:gravity : 表示当前View,即控件,内部的东西的,对齐方式
android:layout_gravity: 表示当前View,即控件本身,在父一级内的(即父一级控件所给当前子控件所分配的显示范围内)的对齐方式
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:text="文字的gravity属性" />
</LinearLayout>
此处android:layout_gravity="right"属性设置无效
(1) 当我们的LinearLayout的orientation的属性值是horizontal也就是水平方向时:--->我们可以设置控件的layout_gravity属性值为:竖直方向的变化比如bottom,top,center_vertical。
(2) 当我们的LinearLayout的orientation的属性值是vertical也就是竖直方向是:--->我们可以设置控件的layout_gravity属性值为:水平方向的变化比如right,left,center_horizontal。
(3)这个结论表明,layout_gravity属性时子控件相对于父布局的相对位置,一旦父布局写死了方向水平或者垂直,在该方向上就只能是默认变化,子控件无法干预,但是在与之相对的方向上子控件可以自由变幻,想想也是可以理解的,老子的方向订好了,你只能是往前一点或者往后一点,想直接达到终点这不行,但是允许你出去找小朋友玩耍~~~
(4)有一个比较特殊的是center,不管是横向还是纵向的时候,它总有一个方向起作用
wrap_content:根据内容所需的尺寸来调整师徒的大小。
match_parent:视图采用其父视图组所允许的最大尺寸。
需要注意的是,这两个属性是有区别的:
android:gravity是指本元素的子元素相对它的对齐方式,
android:layout_gravity是指本元素相对它的父元素的对齐方式。
相同的,对于其他属性,如果加上layout_前缀,就代表着本元素相对父元素的属性。