xml布局文件基本属性
android:layout_above 将该控件置于给定ID的控件之上。
android:layout_below 将该控件置于给定ID的控件之下。
android:layout_toLeftOf 将该控件置于给定ID的控件之左。
android:layout_toRightOf 将该控件置于给定ID的控件之右。
例:android:layout_above=”@id/XXX”
这组属性的后面跟的都是相对控件的ID,表示的都是相对给定ID控件的上下左右方向位置。
android:layout_alignBaseline
android:layout_alignBottom 将该控件的底部边缘与给定ID控件的底部边缘对齐
android:layout_alignLeft 将该控件的左边边缘与给定ID控件的左边边缘对齐
android:layout_alignTop 将该控件的顶部边缘与给定ID控件的顶部边缘对齐
android:layout_alignRight 将该控件的右边边缘与给定ID控件的右边边缘对齐
例:android:layout_alignBotton=”@id/XXX”
这组属性的后面跟的都是相对控件的ID,表示的都是控件自身的上下左右边缘与给定ID控件的对应边缘对齐。外对齐。
android:layout_alignParentBottom 如果值为true,则将该控件的底部与父控件的底部对齐
android:layout_alignParentLeft 如果值为true,则将该控件的左边缘与父控件的左边缘对齐
android:layout_alignParentRight如果值为true,则将该控件的右边缘与父控件的右边缘对齐
android:layout_alignParentTop如果值为true,则将该控件的顶部与父控件的顶部对齐
例:android:layout_alignParentBottom = true
这组属性的后面跟的是true或false,如果不指定,默认就是false,表示的都是控件自身的上下左右边缘与父控件的对应边缘对齐,因为控件是在父控件的内部,所以是内对齐。
android:layout_centerHorizontal 如果值为true,则该控件在其父控件范围内水平居中
android:layout_centerInparent 如果值为true,则该控件在其父控件范围内垂直且水平居中
android:layout_centerVertical如果值为true,则该控件在其父控件范围内垂直居中
例:android:layout_centerHorizontal = true
这组属性的后面跟的是true或false,如果不指定,默认就是false,表示的都是控件自身相对于父控件范围内的居中情况。
horizontally水平地 vertically垂直地
android:layout_marginBottom="3dip"则该控件距离其父控件底部3dip
android:layout_marginLeft="30dip"则该控件距离其父控件左部30dipandroid:layout_marginRight="3dip"则该控件距离其父控件右部3dipandroid:layout_marginTop="3dip"则该控件距离其父控件顶部3dip
当按钮分别设置以上两个属性时,得到的效果是不一样的。
android:paddingLeft="30px"
按钮上设置的内容(例如图片)离按钮本身左边边界30个像素
android:layout_marginLeft="30px"
整个按钮离其父控件左边设置的内容30个像素
android:gravity
android:gravity属性是对该view 内容的限定.比如一个button 上面的text. 你可以设置该text 在view的靠左,靠右等位置.以button为例,android:gravity="right"则button上面的文字靠右
android:layout_gravity
android:layout_gravity是用来设置该view相对与其父view 的位置.比如一个button 在linearlayout里,你想把该button放在靠左、靠右等位置就可以通过该属性设置.以button为例,android:layout_gravity="right"则button靠右
[html] view plain copy
xmlversion="1.0"encoding="utf-8"?>
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Type here:"/>
android:id="@+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:layout_below="@id/label"/>
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/entry"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:text="OK"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/ok"
android:layout_alignTop="@id/ok"
android:text="Cancel"/>
RelativeLayout>
总之一句话:带layout的是向外即此控件距离其父控件的距离,而不带layout的是向内此控件内部的数据设定。