FrameLayout: 不能设计这个控件的位置,控件会放到左上角开始叠加
下为布局代码
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id = "@string/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@string/button1"
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
/>
<Button
android:id="@string/button2"
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
/>
</FrameLayout>
LinearLayout:线性布局,通过制定android:orientation ="horizontal"参数可以让控件摆放纵向或横向
纵向时代码同上把<FrameLayout></FrameLayout>改为<LinearLayout></LinearLayout>即可
横向时将android:orientation 设为"horizontal",textview控件位置放到两个button之后(为什么,呵呵,因为android:layout_width值为"fill_parent"把他改为wrap_content亦可)
TableLayout:这个要和TableRow配合使用,很像html里面的table
每一个TableRow里面自定义控件数放进去就好
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableRow>
<Button android:id="@string/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="两个button的对话框1" />
<Button android:id="@string/button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="三个button的对话框2" />
</TableRow>
<TableRow>
<Button android:id="@string/button3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="可以进行输入的对话框" />
<Button android:id="@string/button4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="进度框" />
</TableRow>
</TableLayout>
AbsoluteLayout:里面可以放多个控件,并且可以自己定义控件的x,y的位置
指定为绝对布局后可以为每个控件指定xy坐标
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation ="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id = "@string/tv"
android:layout_x= "60dip"
android:layout_y="30dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@string/button1"
android:layout_x= "0dip"
android:layout_y="130dip"
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
/>
<Button
android:id="@string/button2"
android:layout_x= "80dip"
android:layout_y="130dip"
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
/>
</AbsoluteLayout>
RelativeLayout:里面可以放多个控件,不过控件的位置都是相对位置
允许子元素指定它们相对于其父元素或兄弟元素的位置
指定为relativelayout后对各个控件要设置相对位置的参数,可以根据id来设置相对哪个控件,android:layout_below="@id/tv"
android:layout_marginTop="20dip"//使得相对位置更精确
也可以直接相对于父容器
android:layout_alignParentRight="true"
具体分类如下:
第一类:属性值为true或false
android:layout_centerHrizontal 水平居中
android:layout_centerVertical 垂直居中
android:layout_centerInparent 相对于父元素完全居中
android:layout_alignParentBottom 贴紧父元素的下边缘
android:layout_alignParentLeft 贴紧父元素的左边缘
android:layout_alignParentRight 贴紧父元素的右边缘
android:layout_alignParentTop 贴紧父元素的上边缘
android:layout_alignWithParentIfMissing 如果对应的兄弟元素找不到的话就以父元素做参照物
第二类:属性值必须为id的引用名“@id/id-name”
android:layout_below 在某元素的下方
android:layout_above 在某元素的的上方
android:layout_toLeftOf 在某元素的左边
android:layout_toRightOf 在某元素的右边
android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐
android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐
android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐
android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐
第三类:属性值为具体的像素值,如30dip,40px
android:layout_marginBottom 离某元素底边缘的距离
android:layout_marginLeft 离某元素左边缘的距离
android:layout_marginRight 离某元素右边缘的距离
android:layout_marginTop 离某元素上边缘的距离
布局代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation ="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id = "@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@string/button1"
android:layout_below="@id/tv"
android:layout_marginTop="20dip"
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
/>
<Button
android:id="@string/button2"
android:layout_alignParentRight="true"
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
/>
</RelativeLayout>