Android 布局管理器(Layout)

本文详细介绍了Android中常见的布局管理器,包括LinearLayout、TableLayout、RelativeLayout、AbsoluteLayout和FrameLayout的使用方法和特点。通过XML布局文件展示了各布局的示例,帮助开发者理解如何创建和配置这些布局。

线性布局: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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值