Android 布局管理器(Layout)
2011-12-20 23:39
155 查看
线性布局:LinerLayout
表格布局:TableLayout
相对布局:RelativeLayout
绝对布局:AbsoluteLayout
帧布局:FrameLayout
定义并展现视图层次的最常用的方法是使用XML布局文件。XML中的每个元素都是View或ViewGroup对象(或他们的子类)。
1.线性布局
注:纵向vertical或横向horizontal
eg:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout//<TextView>没有在该命名空间中,因此其前面无需加上“android:”做前缀
xmlns:android="http://schemas.android.com/apk/res/android" //其属性“xmlns:android”指定命名空间,顶级元素必须指定命名空间。
android:orientation="vertical"//属性“orientation”指定子元素排列方式,其中指定为“vertical”则是子元素垂直排列,而另一个为“horizontal”代表子元素水平排列,
android:layout_width="fill_parent"//“fill_parent”=“match_parent”代表填满其父元素,“wrap_content”代表该元素的大小仅包裹其自身内容
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"//layout_width该元素的宽度
android:layout_height="wrap_content"//layout_height该元素的高度
android:text="@string/name_text"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cancle_button"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ok_button"/>
</LinearLayout>
2.表格布局:
TableLayout属于行和列形式的管理控件,每行为一个TableRow对象,也可以是一个View对象。
在TableRow中还可以继续添加其他的控件,每添加一个子控件就成为一列。TableLayout不会生成边框。
Android:collapseColumns 设置指定的列为collapse,如果一列被标示为collapse,该列会被隐藏
Android:shrinkColumns 设置指定的列为shrinkable,如果一列被标示为shrinkable,列的宽度进行收缩,自适应父容器的大小
Android:stretchColumns 设置指定的列为stretchable,如果一列被标示为stretchable ,该列会被拉伸,填充满表格的空白区域
eg:
<?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="0,1,2,3"//该属性指定每行都由第“0、1、2、3”列占满空白空间
>
<TableRow>//定义一个行
<TextView//定义一个单元格的内容
android:text="@string/name"
android:gravity="center"//指定文字对齐方式
android:padding="3dip" />//指定视图与视图内容间的空隙,单位为像素
<TextView
android:text="@string/gender"
android:gravity="center"
android:padding="3dip" />
<TextView
android:text="@string/age"
android:gravity="center"
android:padding="3dip" />
<TextView
android:text="@string/phonenum"
android:gravity="center"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:text="@string/name1"
android:gravity="center"
android:padding="3dip" />
<TextView
android:text="@string/gender1"
android:gravity="center"
android:padding="3dip" />
<TextView
android:text="@string/age1"
android:gravity="center"
android:padding="3dip" />
<TextView
android:text="@string/phonenum1"
android:gravity="center"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:text="@string/name2"
android:gravity="center"
android:padding="3dip" />
<TextView
android:text="@string/gender1"
android:gravity="center"
android:padding="3dip" />
<TextView
android:text="@string/age2"
android:gravity="center"
android:padding="3dip" />
<TextView
android:text="@string/phonenum2"
android:gravity="center"
android:padding="3dip" />
</TableRow>
</TableLayout>
3.相对布局
相对布局中的视图组件是按相互之间的相对位置来确定的,并不是线性布局中的必须按行或按列单个显示。
eg:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/name_text"
android:id="@+id/text"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/text"//将该元素放到id为text的元素的下面
android:id="@+id/edit"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cancle_button"
android:layout_alignParentRight="true"
android:layout_below="@id/edit"//将该元素放到id为text的元素的下面
android:id="@+id/cancle"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/cancle"//放到id为ok的元素左边
android:layout_alignTop="@id/cancle"//对齐id为ok的元素的顶部
android:text="@string/ok_button"/>
</RelativeLayout>
4.帧布局
帧布局中的每一个组件都代表一个画面,默认以屏幕左上角作为(0,0)坐标,按组件定义的先后顺序依次逐屏显示,后面出现的会覆盖前面的画面。用该布局可以实现动画效果。
eg:
<FrameLayout android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="FrameLayout">
</TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Frame Layout">
</TextView>
</FrameLayout>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/hello" />
5.绝对布局
注:绝对定位AbsoluteLayout,又可以叫做坐标布局,可以直接指定子元素的绝对位置,这种布局简单直接,直观性强,但是由于手机屏幕 尺寸差别比较大,使用绝对定位的适应性会比较差。
layout_x - x 坐标。以左上角为顶点
layout_y - y 坐标。以左上角为顶点
在绝对定位中,如果子元素不设置layout_x和layout_y,那么它们的默认值是0,也就是说它会像在FrameLayout一样这个元素会出现在左上角。
eg:
<?xml version=”1.0″ encoding=”utf-8″?>
<AbsoluteLayout android:id=”@+id/AbsoluteLayout01″
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
xmlns:android=”http://schemas.android.com/apk/res/android”
android:background=”#fff”>
<ImageView
android:src=”@drawable/android”
android:layout_y=”40dip”
android:layout_width=”wrap_content”
android:layout_x=”35dip”
android:id=”@+id/ImageView01″
android:layout_height=”wrap_content”>
</ImageView>
<TextView
android:layout_height=”wrap_content”
android:layout_width=”fill_parent”
android:id=”@+id/TextView01″
android:text=”Android2 课程”
android:textColor=”#0f0″
android:textSize=”28dip”
android:layout_y=”330dip”
android:layout_x=”35dip“>
</TextView>
<TextView
android:layout_height=”wrap_content” //根据控件显示大小
android:layout_width=”fill_parent”//根据屏幕显示大小
android:id=”@+id/TextView02″
android:text=”图文并茂,理论清晰,操作性强”
android:textColor=”#333″
android:textSize=”18dip”
android:layout_y=”365dip”
android:layout_x=”35dip“>
</TextView>
</AbsoluteLayout>
表格布局:TableLayout
相对布局:RelativeLayout
绝对布局:AbsoluteLayout
帧布局:FrameLayout
定义并展现视图层次的最常用的方法是使用XML布局文件。XML中的每个元素都是View或ViewGroup对象(或他们的子类)。
1.线性布局
注:纵向vertical或横向horizontal
eg:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout//<TextView>没有在该命名空间中,因此其前面无需加上“android:”做前缀
xmlns:android="http://schemas.android.com/apk/res/android" //其属性“xmlns:android”指定命名空间,顶级元素必须指定命名空间。
android:orientation="vertical"//属性“orientation”指定子元素排列方式,其中指定为“vertical”则是子元素垂直排列,而另一个为“horizontal”代表子元素水平排列,
android:layout_width="fill_parent"//“fill_parent”=“match_parent”代表填满其父元素,“wrap_content”代表该元素的大小仅包裹其自身内容
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"//layout_width该元素的宽度
android:layout_height="wrap_content"//layout_height该元素的高度
android:text="@string/name_text"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cancle_button"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ok_button"/>
</LinearLayout>
2.表格布局:
TableLayout属于行和列形式的管理控件,每行为一个TableRow对象,也可以是一个View对象。
在TableRow中还可以继续添加其他的控件,每添加一个子控件就成为一列。TableLayout不会生成边框。
Android:collapseColumns 设置指定的列为collapse,如果一列被标示为collapse,该列会被隐藏
Android:shrinkColumns 设置指定的列为shrinkable,如果一列被标示为shrinkable,列的宽度进行收缩,自适应父容器的大小
Android:stretchColumns 设置指定的列为stretchable,如果一列被标示为stretchable ,该列会被拉伸,填充满表格的空白区域
eg:
<?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="0,1,2,3"//该属性指定每行都由第“0、1、2、3”列占满空白空间
>
<TableRow>//定义一个行
<TextView//定义一个单元格的内容
android:text="@string/name"
android:gravity="center"//指定文字对齐方式
android:padding="3dip" />//指定视图与视图内容间的空隙,单位为像素
<TextView
android:text="@string/gender"
android:gravity="center"
android:padding="3dip" />
<TextView
android:text="@string/age"
android:gravity="center"
android:padding="3dip" />
<TextView
android:text="@string/phonenum"
android:gravity="center"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:text="@string/name1"
android:gravity="center"
android:padding="3dip" />
<TextView
android:text="@string/gender1"
android:gravity="center"
android:padding="3dip" />
<TextView
android:text="@string/age1"
android:gravity="center"
android:padding="3dip" />
<TextView
android:text="@string/phonenum1"
android:gravity="center"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:text="@string/name2"
android:gravity="center"
android:padding="3dip" />
<TextView
android:text="@string/gender1"
android:gravity="center"
android:padding="3dip" />
<TextView
android:text="@string/age2"
android:gravity="center"
android:padding="3dip" />
<TextView
android:text="@string/phonenum2"
android:gravity="center"
android:padding="3dip" />
</TableRow>
</TableLayout>
3.相对布局
相对布局中的视图组件是按相互之间的相对位置来确定的,并不是线性布局中的必须按行或按列单个显示。
eg:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/name_text"
android:id="@+id/text"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/text"//将该元素放到id为text的元素的下面
android:id="@+id/edit"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cancle_button"
android:layout_alignParentRight="true"
android:layout_below="@id/edit"//将该元素放到id为text的元素的下面
android:id="@+id/cancle"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/cancle"//放到id为ok的元素左边
android:layout_alignTop="@id/cancle"//对齐id为ok的元素的顶部
android:text="@string/ok_button"/>
</RelativeLayout>
4.帧布局
帧布局中的每一个组件都代表一个画面,默认以屏幕左上角作为(0,0)坐标,按组件定义的先后顺序依次逐屏显示,后面出现的会覆盖前面的画面。用该布局可以实现动画效果。
eg:
<FrameLayout android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="FrameLayout">
</TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Frame Layout">
</TextView>
</FrameLayout>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/hello" />
5.绝对布局
注:绝对定位AbsoluteLayout,又可以叫做坐标布局,可以直接指定子元素的绝对位置,这种布局简单直接,直观性强,但是由于手机屏幕 尺寸差别比较大,使用绝对定位的适应性会比较差。
layout_x - x 坐标。以左上角为顶点
layout_y - y 坐标。以左上角为顶点
在绝对定位中,如果子元素不设置layout_x和layout_y,那么它们的默认值是0,也就是说它会像在FrameLayout一样这个元素会出现在左上角。
eg:
<?xml version=”1.0″ encoding=”utf-8″?>
<AbsoluteLayout android:id=”@+id/AbsoluteLayout01″
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
xmlns:android=”http://schemas.android.com/apk/res/android”
android:background=”#fff”>
<ImageView
android:src=”@drawable/android”
android:layout_y=”40dip”
android:layout_width=”wrap_content”
android:layout_x=”35dip”
android:id=”@+id/ImageView01″
android:layout_height=”wrap_content”>
</ImageView>
<TextView
android:layout_height=”wrap_content”
android:layout_width=”fill_parent”
android:id=”@+id/TextView01″
android:text=”Android2 课程”
android:textColor=”#0f0″
android:textSize=”28dip”
android:layout_y=”330dip”
android:layout_x=”35dip“>
</TextView>
<TextView
android:layout_height=”wrap_content” //根据控件显示大小
android:layout_width=”fill_parent”//根据屏幕显示大小
android:id=”@+id/TextView02″
android:text=”图文并茂,理论清晰,操作性强”
android:textColor=”#333″
android:textSize=”18dip”
android:layout_y=”365dip”
android:layout_x=”35dip“>
</TextView>
</AbsoluteLayout>
相关文章推荐
- android 布局中 layout_gravity、gravity、orientation、layout_weight【转】
- Android开发中如何匹配layout资源(layout-sw480dp layout-sw600dp-land layout-sw720dp-port)
- where can I find source of com.android.internal.R.styleable.AlertDialog_multiChoiceItemLayout?
- 【android初学日志】gravity 和 layout_gravity区别<三>
- Android ApiDemos示例解析(164):Views->Layouts->TableLayout->09. Toggle Shrink
- android布局中属性:scaleType、gravity、layout_gravity
- Android典型界面设计(7) ——DrawerLayout+Fragement+ViewPager+PagerTabStrip实现双导航
- android CoordinatorLayout使用
- Android中layout过程详解 (结合Android 4.0.4 最新源码)
- Android -Logcat:you must supply a layout_width attribute
- Android:Layout_weight的深刻理解
- android屏幕适配autolayout
- Android FoldingLayout 折叠布局 原理及实现(二)
- Android 瀑布流容器WaterFallLayout实现
- 淘宝、天猫用的Android框架V-Layout,赶紧收藏
- 【Android布局】在程序中设置android:gravity 和 android:layout_Gravity属性
- android SwipeRefreshLayout google自己的下拉刷新控件 (需要下载新的v4包)
- Android layout属性大全
- android在Layout中使用分割线
- Android LayoutInflater的使用