Android布局管理器

        在android程序中,我们需要为每一个组件在容器中设定一个位置和大小。而在很多场合我们都很难确定其位置和大小。布局管理器提供了在android程序中更为方便也更为有效的控制方法。

        常用的android布局管理器有线性布局管理器(LinearLayout)、绝对布局管理器(AbsoluteLayout)、相对布局管理器(RelativeLayout)、框架布局管理器(FrameLayout)、表格布局管理器(TableLayout)。

        线性布局管理器(LinearLayout):

                该布局管理器表示,放入其中的所有组件按着水平(horizontal)或垂直(vertical)排列,(默认horizontal)就算超出显示器显示范围也不换行。

                比如在LinearLayout中放入五个按钮:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.amia.layoutexample.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第一个"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第二个"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第三个"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第四个"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第五个"/>
</LinearLayout>

                在程序中运行的结果是这样的:


                可以看到第五个按钮已经超出屏幕,并且没有换行,所以无法显示。

                LinearLayout是垂直排列还是水平排列可以在android:orientation中设置:

android:orientation="vertical"

                可以使其其变为垂直排列:


        绝对布局管理器(AbsoluteLayout):

                放置在其中的任何一个组件都要指明其精确坐标。不过在新版本的sdk中已经过期(本人用的是API21),不再使用。

        相对布局管理器(RelativeLayout):

                RelativeLayout是按组件的相对位置来布局。简单来说就是可以设定一个组件在另一个组件的上还是下,左还是右。

                如要在屏幕中间设置一个按钮,其周围各放置一个按钮,可以在xml中这样写:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.amia.layoutexample.MainActivity">

    <Button
        android:layout_centerInParent="true"
        android:id="@+id/but1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="中央"/>
    <Button
        android:layout_alignRight="@id/but1"
        android:layout_above="@id/but1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="上方"/>
    <Button
        android:layout_alignRight="@id/but1"
        android:layout_below="@id/but1"
        android:text="下方"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:layout_alignBottom="@id/but1"
        android:layout_toLeftOf="@id/but1"
        android:text="左方"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
    android:layout_alignTop="@id/but1"
    android:text="右方"
    android:layout_toRightOf="@id/but1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</RelativeLayout>

                layout_centerInParent是指定其是否放置在容器的中央位置;

                layout_alignRight表示该组件和指定组件的右边界对齐;

                layout_above表示该组件在指定组件的上方;

                layout_below表示该组件在指定组件的下方;

                layout_alignButtom表示该组件与指定组件的下边界对齐;

                layout_toLeftOf表示该组件在指定组件的左方;

                layout_toRightOf表示该组件在指定组件的右方;

                layout_alignTop表示该组件在与指定组件的上边界对齐。

                结果如下:


        框架布局管理器(FrameLayout)

                允许组件重叠的布局管理器。默认情况从(0,0)开始布局。由于没加入一个组件都会创建一个空白区域,通常称为一帧,故又被称为帧布局管理器。

                假设我们需要在界面中放置几个重叠的组件,xml代码可以这么写:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.amia.layoutexample.MainActivity">

    <Button
        android:background="#000000"
        android:id="@+id/but1"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_gravity="center" />
    <Button
        android:background="#00FF00"
        android:layout_gravity="center"
        android:layout_width="200dp"
        android:layout_height="200dp" />
    <Button
        android:background="#0000FF"
        android:layout_marginTop="50dp"
        android:layout_width="100dp"
        android:layout_height="100dp" />
</FrameLayout>

                layout_gravity表示其相对于其容器的位置,指定为center表示它在该布局管理器的中央;同时也可以利用距离其容器各个边的距离来指定其位置,如layout_marginTop表示该组件和容器顶部的距离。

                效果如下:


        表格布局管理器(TableLayout):

                表格布局管理可以将容器分割为许多子部分(如同表格一样),在”表格“的指定位置放置需要的组件。

                比如,我们在第一行放置一个输入用户名的输入框,第二行放置一个确定按钮,代码可以这么写:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.amia.layoutexample.MainActivity">

    <TableRow android:id="@+id/firstRow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView android:text="用户名:"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <EditText android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:text="输入用户名:"
            />
    </TableRow>
    <TableRow android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/but1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="确定" />
    </TableRow>
</TableLayout>

                每一个TableRow表示一行,每一行中可以放置一个或多个组件。效果如下:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值