您的位置:首页 > 移动开发 > Android开发

Android开发之常用布局

2015-06-22 22:41 441 查看
1.LinearLayout

布局中最简单的一种,也是比较好用的一种,放在混合布局里有时可以简化布局。



<?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="vertical" >
<TextView
android:text="name:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>

</LinearLayout>
主要常用设置的属性:

android:orientation:设置布局的垂直(vertical)或水平(horizontal)。

2.TableLayout



跟html的table布局差不多,省略了列的标识,由tablerow来规划。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1" >
<TableRow>
<TextView
android:text="URL"/>
<EditText
android:id="@+id/edittext"
android:layout_span="3"  />
</TableRow>
<TableRow>
<Button
android:id="@+id/button1"
android:text="cancel"
android:layout_column="2"  />
<Button
android:id="@+id/button2"
android:text="ok"/>

</TableRow>

</TableLayout>


TableLayout可设置的属性包括全局属性及单元格属性。

1、全局属性也即列属性,有以下3个参数:

android:stretchColumns    设置可伸展的列。该列可以向行方向伸展,最多可占据一整行。

android:shrinkColumns     设置可收缩的列。当该列子控件的内容太多,已经挤满所在行,那么该子控件的内容将往列方向显示。

android:collapseColumns 设置要隐藏的列。

示例:

android:stretchColumns="0"           第0列可伸展

android:shrinkColumns="1,2"         第1,2列皆可收缩

android:collapseColumns="*"         隐藏所有行

说明:列可以同时具备stretchColumns及shrinkColumns属性,若此,那么当该列的内容N多时,将“多行”显示其内容。(这里不是真正的多行,而是系统根据需要自动调节该行的layout_height)

2、单元格属性,有以下2个参数:

android:layout_column    指定该单元格在第几列显示

android:layout_span        指定该单元格占据的列数(未指定时,为1)

示例:

android:layout_column="1"    该控件显示在第1列

android:layout_span="2"        该控件占据2列

说明:一个控件也可以同时具备这两个特性。

3.RelativeLayout



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/label"
android:text="URL:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/edit"
android:layout_alignParentLeft="true"/>
<EditText
android:id="@+id/edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/label"
android:layout_alignParentTop="true"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5px"
android:layout_below="@id/edit"
android:layout_alignParentRight="true">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="cancel"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ok"/>

</LinearLayout>
</RelativeLayout>


android:layout_above="@id/xxx"  --将控件置于给定ID控件之上

android:layout_below="@id/xxx"  --将控件置于给定ID控件之下

android:layout_toLeftOf="@id/xxx"  --将控件的右边缘和给定ID控件的左边缘对齐

android:layout_toRightOf="@id/xxx"  --将控件的左边缘和给定ID控件的右边缘对齐

android:layout_alignLeft="@id/xxx"  --将控件的左边缘和给定ID控件的左边缘对齐

android:layout_alignTop="@id/xxx"  --将控件的上边缘和给定ID控件的上边缘对齐

android:layout_alignRight="@id/xxx"  --将控件的右边缘和给定ID控件的右边缘对齐

android:layout_alignBottom="@id/xxx"  --将控件的底边缘和给定ID控件的底边缘对齐

android:layout_alignParentLeft="true"  --将控件的左边缘和父控件的左边缘对齐

android:layout_alignParentTop="true"  --将控件的上边缘和父控件的上边缘对齐

android:layout_alignParentRight="true"  --将控件的右边缘和父控件的右边缘对齐

android:layout_alignParentBottom="true" --将控件的底边缘和父控件的底边缘对齐

android:layout_centerInParent="true"  --将控件置于父控件的中心位置

android:layout_centerHorizontal="true"  --将控件置于水平方向的中心位置

android:layout_centerVertical="true"  --将控件置于垂直方向的中心位置
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: