欢迎光临散文网 会员登陆 & 注册

第五章 用户界面基础(界面布局管理器)

2018-11-06 20:54 作者:swiss126  | 我要投稿

参考资料:

《Android应用程序开发》ISBN 9787302283164

参考软件:

Android Studio、Eclipse+ADT、Android SDK、JDK

界面布局管理器

android layout布局属性

http://blog.csdn.net/msmile_my/article/details/9018775

Android系统五大布局详解Layout

http://blog.csdn.net/llping2011/article/details/9992941

1、线性布局

线性布局是程序中最常见的一种布局方式,线性布局可以分为水平线性布局和垂直线性布局两种, 通过android:orientation属性可以设置线性布局的方向

    在LinearLayout中设置排列方式为水平时只有垂直方向的设置是有效的,水平方向的设置是无效的:即left,right,center_horizontal 是不生效的

   在LinearLayout中设置排列方式为垂直时只有水平方向设置是有效的,垂直方向的设置是无效的是无效的:即top,bottom,center_vertical 是无效的;

android:orientation="vertical"表示竖直方式对齐  

android:orientation="horizontal"表示水平方式对齐

示例:

2、框架布局

http://blog.csdn.net/neu_yousei/article/details/22158559

http://blog.csdn.net/eana_don/article/details/8332778

框架布局管理器是将组件都放在屏幕的左上角,所有的组件是层叠显示的

示例:

<?xml version="1.0"  encoding="utf-8"?>

<FrameLayout  xmlns:android="http://schemas.android.com/apk/res/android"

     android:layout_width="fill_parent"

     android:layout_height="fill_parent"

     android:orientation="vertical" >

    <ImageView

         android:id="@+id/img"

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:contentDescription="这是一个图片"

         android:src="@drawable/ic_launcher" />

    <TextView

         android:id="@+id/text"

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:text="这是提示文字" />

    <Button  android:id="@+id/btn"

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:text="这是按钮"/>

</FrameLayout>

3、表格布局

TableLayout表格布局详解

http://blog.sina.com.cn/s/blog_63c66eb60100u29p.html

 

TableLayout跟TableRow 是一组搭配应用的布局,TableLayout置底,TableRow在TableLayout的上方,而Button、TextView等控件就在TableRow之上,别的,TableLayout之上也可以零丁放控件。TableLayout是一个应用错杂的布局,最简单的用法就仅仅是拖沓控件做出个界面,但实际上,会经常在代码里应用TableLayout,例如做出表格的结果。本文首要介绍TableLayout的根蒂根基应用办法。 

1.android:collapseColumns://隐藏指定的列
        ①设置 TableLayout 内的 TableRow 中需要隐藏的列的列索引,多个用“,”隔开        ②以第0行为序,隐藏指定的列:把android:collapseColumns=0,3意思是把第0和第3列隐藏

 
    2.android:shrinkColumns://收缩指定的列以适合屏幕、不会挤出屏幕                                   ① 设置 TableLayout 内的 TableRow 中需要拉伸(该列会拉伸到所有可用空间)的列的列索引,多列个用“,”隔开(多列每列填充空隙大小一样)       ②以第0行为序,自动延伸指定的列填充可用部分: 当LayoutRow里面的控件还没有布满布局时,shrinkColumns不起作用。     ③设置了shrinkColumns=1,4,布局完全没有改变,因为LayoutRow里面还剩足够的空间。当LayoutRow布满控件时,设置了shrinkColumns=2,5,则控件自动向垂直方向填充空间 

                                                                                                                3.android:stretchColumns://尽量把指定的列表填充空白部分                    

      ①设置 TableLayout 内的 TableRow 中需要收缩(为了使其他列不会被挤到屏幕 外,此列会自动收缩)的列的列索引,多个用“,”隔开 

        ② 以第0行为序,尽量把指定的列填充空白部分:设置stretchColumns=2,5,第1,4列被尽量填充同时向右填充,直到2,5被压挤到最后边)。 

补充:

①表格布局的子对象不能指定 layout_width 属性.宽度永远是 MATCH_PARENT。

②不过子对象可以定义 layout_height 属性;其默认值是WRAP_CONTENT. 如果子对象是 TableRow,其高度永远是 WRAP_CONTENT。

实例:

<LinearLayoutxmlns: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"

    android:orientation="vertical"

    tools:context=".AndroidTableLayoutActivity">

    <!-- 定义第一个表格,指定第2列允许收缩,第3列允许拉伸 -->

    <TableLayout 

        android:id="@+id/tablelayout01"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:shrinkColumns="1"

        android:stretchColumns="2">

        <!-- 直接添加按钮,自己占用一行 -->

        <Button

            android:id="@+id/btn01"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="独自一行">

        </Button>

        <TableRow>

        <Button

                android:id="@+id/btn02"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="普通">

            </Button>

            <Button

                android:id="@+id/btn03"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

               android:text="允许被收缩允许被收缩允许被收缩允许被收缩">

            </Button>

            <Button

                android:id="@+id/btn04"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="允许被拉伸允许被拉伸允许被拉伸">

            </Button>

        </TableRow>

    </TableLayout>

    <!-- 定义第2个表格,指定第2列隐藏 -->

    <TableLayout

        android:id="@+id/tablelayout02"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:collapseColumns="1">

        <TableRow>

            <Button

                android:id="@+id/btn05"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="普通">

            </Button>

            <Button

                android:id="@+id/btn06"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="被隐藏列">

            </Button>

            <Button

                android:id="@+id/btn07"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="允许被拉伸">

            </Button>

        </TableRow>

    </TableLayout>

    <!-- 定义第3个表格,指定第2列填满空白 -->

    <TableLayout

        android:id="@+id/tablelayout03"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:stretchColumns="1">

        <TableRow>

            <Button

                android:id="@+id/btn08"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="普通">

            </Button>

            <Button

                android:id="@+id/btn09"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="填满剩余空白">

            </Button>

        </TableRow>

    </TableLayout>

    <!-- 定义第3个表格,指定第2列横跨2列 -->

    <TableLayout

        android:id="@+id/tablelayout04"

        android:layout_width="match_parent"

        android:layout_height="wrap_content">

        <TableRow>

            <Button

                android:id="@+id/btn10"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="普通">

            </Button>

            <Button

                android:id="@+id/btn11"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_column="2"

                android:text="填满剩余空白">

            </Button>

        </TableRow>

    </TableLayout>

</LinearLayout>

 4、相对布局

http://blog.csdn.net/zenglinkai/article/details/10156773

  相对布局(RelativeLayout)是一种非常灵活的布局方式,能够通过指定界面元素与其他元素的相对位置关系,确定界面中所有元素的布局位置

 特点:能够最大程度保证在各种屏幕尺寸的手机上正确显示界面布局

AndroidRelativeLayout 属性

// 相对于给定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的右边缘对齐;

// 相对于父组件

android:layout_alignParentTop     如果为true,将该控件的顶部与其父控件的顶部对齐;

android:layout_alignParentBottom 如果为true,将该控件的底部与其父控件的底部对齐;

android:layout_alignParentLeft     如果为true,将该控件的左部与其父控件的左部对齐;

android:layout_alignParentRight    如果为true,将该控件的右部与其父控件的右部对齐;

// 居中

android:layout_centerHorizontal 如果为true,将该控件的置于水平居中;

android:layout_centerVertical    如果为true,将该控件的置于垂直居中;

android:layout_centerInParent   如果为true,将该控件的置于父控件的中央;

// 指定移动像素

android:layout_marginTop     上偏移的值;

android:layout_marginBottom 下偏移的值;

android:layout_marginLeft   左偏移的值;

android:layout_marginRight 右偏移的值;

5、绝对布局

 绝对布局(AbsoluteLayout)能通过指定界面元素的坐标位置,来确定用户界面的整体布局

 绝对布局是一种不推荐使用的界面布局,因为通过X轴和Y轴确定界面元素位置后,Android系统不能够根据不同屏幕对界面元素的位置进行调整,降低了界面布局对不同类型和尺寸屏幕的适应能力

每一个界面控件都必须指定坐标(X,Y),例如“确认”按钮的坐标是(40,120),“取消”按钮的坐标是(120,120)。坐标原点(0,0)在屏幕的左上角

 

6、网格布局

http://www.cnblogs.com/skywang12345/p/3154150.html

  网格布局(GridLayout)

 Android SDK 4.0新支持的布局方式

将用户界面划分为网格,界面元素可随意摆放在网格中

 网格布局比表格布局(TableLayout)在界面设计上更加灵活,在网格布局中界面元素可以占用多个网格的,而在表格布局却无法实现,只能将界面元素指定在一个表格行(TableRow)中,不能跨越多个表格行。

GroidLayoutDemo示例说明网格布局的使用方法

 在Eclipse界面设计器中的界面图示和在Android模拟器运行后的用户界面。

7、约束布局

http://blog.csdn.net/u012930328/article/details/52048531


第五章 用户界面基础(界面布局管理器)的评论 (共 条)

分享到微博请遵守国家法律