Android应用软件开发

194课时
693学过
8分

课程评价 (0)

请对课程作出评价:
0/300

学员评价

暂无精选评价
3分钟

2.2 相关知识

相关知识

Android六大基本布局分别是:线性布局LinearLayout、表格布局TableLayout、相对布局RelativeLayout、层布局FrameLayout、绝对布局AbsoluteLayout、网格布局GridLayout。其中,表格布局是线性布局的子类。网格布局是android 4.0后新增的布局。在手机程序设计中,绝对布局基本上不用,用得相对较多的是线性布局和相对布局。

RelativeLayout相对布局

相对布局可以让子控件相对于兄弟控件或父控件进行布局,可以设置子控件相对于兄弟控件或父控件进行上下左右对齐。

RelativeLayout能替换一些嵌套视图,当我们用LinearLayout来实现一个简单的布局但又使用了过多的嵌套时,就可以考虑使用RelativeLayout重新布局。相对布局一定要加Id才能管理。

RelativeLayout中子控件常用属性:

1.相对于父控件,例如:android:layout_alignParentTop=“true”

android:layout_alignParentTop 控件的顶部与父控件的顶部对齐;

android:layout_alignParentBottom 控件的底部与父控件的底部对齐;

android:layout_alignParentLeft 控件的左部与父控件的左部对齐;

android:layout_alignParentRight 控件的右部与父控件的右部对齐;

2.相对给定Id控件,例如:android:layout_above=“@id/”

android:layout_above 控件的底部置于给定ID的控件之上;

android:layout_below 控件的底部置于给定ID的控件之下;

android:layout_toLeftOf 控件的右边缘与给定ID的控件左边缘对齐;

android:layout_toRightOf 控件的左边缘与给定ID的控件右边缘对齐;

android:layout_alignBaseline 控件的baseline与给定ID的baseline对齐;

android:layout_alignTop 控件的顶部边缘与给定ID的顶部边缘对齐;

android:layout_alignBottom 控件的底部边缘与给定ID的底部边缘对齐;

android:layout_alignLeft 控件的左边缘与给定ID的左边缘对齐;

android:layout_alignRight 控件的右边缘与给定ID的右边缘对齐;

3.居中,例如:android:layout_centerInParent=“true”

android:layout_centerHorizontal 水平居中;

android:layout_centerVertical 垂直居中;

android:layout_centerInParent 父控件的中央;

表2-2-2 Ex2_2 布局文件ex_relativielayout.xml清单

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="center"/>

    <Button
        android:id="@+id/button_above"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button_center"
        android:layout_centerInParent="true"
        android:text="above"/>

    <Button
        android:id="@+id/button_below"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button_center"
        android:layout_centerInParent="true"
        android:text="below"/>

    <Button
        android:id="@+id/button_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/button_center"
        android:layout_centerVertical="true"
        android:text="left"/>

    <Button
        android:id="@+id/button_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/button_center"
        android:layout_centerVertical="true"
        android:text="right"/>

</RelativeLayout>

效果如图2-2-3

16

图2-2-3 相对布局

TableLayout表格布局

表格布局,适用于多行多列的布局格式,每个TableLayout是由多个TableRow组成,一个TableRow就表示TableLayout中的每一行,这一行可以由多个子元素组成。实际上TableLayout和TableRow都是LineLayout线性布局的子类。但是TableRow的参数android:orientation属性值固定为horizontal,且android:layout_width=MATCH_PARENT,android:layout_height=WRAP_CONTENT。所以TableRow实际是一个横向的线性布局,且所以子元素宽度和高度一致。

TableLayout常用属性:

android:shrinkColumns:设置可收缩的列,内容过多就收缩显示到第二行

android:stretchColumns:设置可伸展的列,将空白区域填充满整个列

android:collapseColumns:设置要隐藏的列

列的索引从0开始,shrinkColumns和stretchColumns可以同时设置。

子控件常用属性:

android:layout_column:第几列

android:layout_span:占据列数

表2-2-3 Ex2_2 布局文件ex_tablelayout.xml清单

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="首页"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center">

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:stretchColumns="0,1,2"
            android:gravity="center">

            <TableRow>
                <TextView
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_margin="5dp"
                    android:background="#e2a617"
                    android:text="文件管理"
                    android:gravity="center"/>

                <TextView
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_margin="5dp"
                    android:background="#0d637f"
                    android:text="应用商店"
                    android:gravity="center"/>

                <TextView
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_margin="5dp"
                    android:background="#aa2266"
                    android:text="文件管理"
                    android:gravity="center"/>
            </TableRow>

            <TableRow>
                <TextView
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_margin="5dp"
                    android:background="#45e15f"
                    android:text="应用管理"
                    android:gravity="center"/>
                <TextView
                    android:layout_width="200dp"
                    android:layout_height="100dp"
                    android:layout_margin="5dp"
                    android:background="#3924a4"
                    android:text="应用中心"
                    android:gravity="center"
                    android:layout_span="2"/>
            </TableRow>

        </TableLayout>

    </LinearLayout>

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:background="#f5f5f5"
        android:stretchColumns="0,1,2,3"
        android:gravity="center_vertical">

        <TableRow>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="首页" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="消息" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="发现" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="我" />
        </TableRow>
    </TableLayout>
</LinearLayout>

效果如图2-2-4所示。

17