Android  UI基础之五大布局

 Android的界面是有布局和组件协同完成的,布局好比是建筑里的框架,而组件则相当于建筑里的砖瓦。组件按照布局的要求依次排列,就组成了用户所看见的界面。Android的五大布局分别是LinearLayout(线性布局)、FrameLayout(单帧布局)、RelativeLayout(相对布局)、AbsoluteLayout(绝对布局,Android2.0中标注为已过期)和TableLayout(表格布局)

LinearLayout

线性布局

子元素任意

FrameLayout

帧布局

子元素任意

TableLayout

表格布局

子元素为<TableRow>,其中可放各种控件

RelativeLayout

相对布局

子元素任意

AbsoluteLayout

绝对布局

子元素任意

公共控件属性:

android:id=

"@+id/***"

控件id

android:layout_width=

android:layout_height=

"wrap_content"

"match_parent"

"fill_parent"

控件的宽度

控件的高度

android:layout_marginLeft=

android:layout_marginRight=

android:layout_marginTop=

android:layout_marginBottom=

“5dip”

控件各边距离其他控件的距离

android:text=

"***"

控件的文本内容

android:textSize=

控件的文本大小

android:textColor=

“#FFFFFFFF”

控件的文本颜色

android:textStyle =

"normal"/"bold"/"italic"

控件的文本格式

android:background =

“@drawable/background”

“#FFFFFFFF”

控件的背景(图片或颜色)

android:src =

“@drawable/image”

控件的图片

android:visibility=

"visible"

"invisible"

"gone"

"visible"表可见,

"invisible"表不可见,但在布局中占用的位置还在,

"gone"表不可见,完全从布局中消失

android:padding=

“5dip”

控件内容距离控件边界的距离

android:weight=    

控件占有的权重

android:ems=

android:maxEms=

android:minEms=

空间的宽度

android:gravity=

“top”,”left”,”start”,”fill”,”center”…

控件元素在控件显示的位置

一、LinearLayout

LinearLayout重要属性

android:orientation=

“horizontal”

“vertaical”

布局中子控件排布方向

  LinearLayout按照垂直或者水平的顺序依次排列子元素,每一个子元素都位于前一个元素之后。如果是垂直排列,那么将是一个N行单列的结构,每一行只会有一个元素,而不论这个元素的宽度为多少;如果是水平排列,那么将是一个单行N列的结构。如果搭建两行两列的结构,通常的方式是先垂直排列两个元素,每一个元素里再包含一个LinearLayout进行水平排列。

  LinearLayout中的子元素属性android:layout_weight生效,它用于描述该子元素在剩余空间中占有的大小比例。加入一行只有一个文本框,那么它的默认值就为0,如果一行中有两个等长的文本框,那么他们的android:layout_weight值可以是同为1。如果一行中有两个不等长的文本框,那么他们的android:layout_weight值分别为1和2,那么第一个文本框将占据剩余空间的三分之二,第二个文本框将占据剩余空间中的三分之一。android:layout_weight遵循数值越小,重要度越高的原则。显示效果如下:

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

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

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent">

     <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:background="#ff000000"

        android:text="@string/hello"/>

    <LinearLayout

        android:orientation="horizontal"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent">

         <TextView

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:background="#ff654321"

            android:layout_weight=""

            android:text=""/>

        <TextView

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:background="#fffedcba"

            android:layout_weight=""

            android:text=""/>

    </LinearLayout>

</LinearLayout>

二、FrameLayout:

  FrameLayout是五大布局中最简单的一个布局,在这个布局中,整个界面被当成一块空白备用区域,所有的子元素都不能被指定放置的位置,它们统统放于这块区域的左上角,并且后面的子元素直接覆盖在前面的子元素之上,将前面的子元素部分和全部遮挡。显示效果如下,第一个TextView被第二个TextView完全遮挡,第三个TextView遮挡了第二个TextView的部分位置。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff000000"
android:gravity="center"
android:text=""/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff654321"
android:gravity="center"
android:text=""/>
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#fffedcba"
android:gravity="center"
android:text=""/>
</FrameLayout>

 

三、AbsoluteLayout:(Android2.0中标注为已过期,不推荐使用)

AbsoluteLayout重要属性

android:layout_X/Y

 

控件的X,Y坐标

AbsoluteLayout是绝对位置布局。在此布局中的子元素的android:layout_x和android:layout_y属性将生效,用于描述该子元素的坐标位置。屏幕左上角为坐标原点(0,0),第一个0代表横坐标,向右移动此值增大,第二个0代表纵坐标,向下移动,此值增大。在此布局中的子元素可以相互重叠。在实际开发中,通常不采用此布局格式,因为它的界面代码过于刚性,以至于有可能不能很好的适配各种终端。显示效果如下:

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

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

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent">

    <TextView

        android:layout_width="50dp"

        android:layout_height="50dp"

        android:background="#ffffffff"

        android:gravity="center"

        android:layout_x="50dp"

        android:layout_y="50dp"

        android:text=""/>

    <TextView

        android:layout_width="50dp"

        android:layout_height="50dp"

        android:background="#ff654321"

        android:gravity="center"

        android:layout_x="25dp"

        android:layout_y="25dp"

        android:text=""/>

    <TextView

        android:layout_width="50dp"

        android:layout_height="50dp"

        android:background="#fffedcba"

        android:gravity="center"

        android:layout_x="125dp"

        android:layout_y="125dp"

        android:text=""/>

</AbsoluteLayout>

 

四、RelativeLayout

RelativeLayout重要属性

android:layout_alignParentLeft

android:layout_alignParentRight

android:layout_alignParentTop

android:layout_alignParentBottom

“true”

“false”

控件在布局中的相对位置

android:orientation 

“horizontal”

“vertaical”

布局中子控件排布方向

android:layout_toRightOf

android:layout_toLeftOf

android:layout_below

android:layout_above

“@+id/***”

控件和某个控件的位置关系

android:layout_alignTop

android:layout_alignBottom

android:layout_alignLeft

android:layout_alignRight

android:layout_alignBaseline

“@+id/***”

控件与其他控件对齐

android:layout_centerHorizontal

android:layout_centerVirtical

android:layout_centerInParent

指定控件位于水平/垂直/父控件的中间位置

  RelativeLayout按照各子元素之间的位置关系完成布局。在此布局中的子元素里与位置相关的属性将生效。例如android:layout_below, android:layout_above等。子元素就通过这些属性和各自的ID配合指定位置关系。注意在指定位置关系时,引用的ID必须在引用之前,先被定义,否则将出现异常。

  RelativeLayout是Android五大布局结构中最灵活的一种布局结构,比较适合一些复杂界面的布局。下面示例就展示这么一个情况,第一个文本框与父组件的底部对齐,第二个文本框位于第一个文本框的上方,并且第三个文本框位于第二个文本框的左方。

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

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

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent">

    <TextView 

        android:id="@+id/text_01" 

        android:layout_width="50dp"

        android:layout_height="50dp"

        android:background="#ffffffff"

        android:gravity="center"

        android:layout_alignParentBottom="true" 

        android:text=""/>

    <TextView 

        android:id="@+id/text_02"

        android:layout_width="50dp"

        android:layout_height="50dp"

        android:background="#ff654321"

        android:gravity="center"

        android:layout_above="@id/text_01" 

        android:layout_centerHorizontal="true" 

        android:text=""/>

    <TextView 

        android:id="@+id/text_03"

        android:layout_width="50dp"

        android:layout_height="50dp" 

        android:background="#fffedcba"

        android:gravity="center"

        android:layout_toLeftOf="@id/text_02"

        android:layout_above="@id/text_01" 

        android:text=""/>

</RelativeLayout>

五、TableLayout

TableLayout重要属性

android:stretchColums

 

指定当一个TableRow中的控件不够填充满整个宽度时,将会被拉伸的条目

TableLayout顾名思义,此布局为表格布局,适用于N行N列的布局格式。一个TableLayout由许多TableRow组成,一个TableRow就代表TableLayout中的一行。

  TableRow是LinearLayout的子类,它的android:orientation属性值恒为horizontal,并且它的android:layout_width和android:layout_height属性值恒为MATCH_PARENT和WRAP_CONTENT。所以它的子元素都是横向排列,并且宽高一致的。这样的设计使得每个TableRow里的子元素都相当于表格中的单元格一样。在TableRow中,单元格可以为空,但是不能跨列。

  下面示例演示了一个TableLayout的布局结构,其中第二行只有两个单元格,而其余行都是三个单元格。

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

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

    android:orientation="vertical" 

    android:layout_width="fill_parent" 

    android:layout_height="fill_parent">

    <TableRow 

        android:layout_width="fill_parent"

        android:layout_height="wrap_content">

        <TextView  

            android:background="#ffffffff"

            android:gravity="center" 

            android:padding="10dp"

            android:text=""/>

        <TextView 

            android:background="#ff654321"

            android:gravity="center"

            android:padding="10dp" 

            android:text=""/>

        <TextView  

            android:background="#fffedcba" 

            android:gravity="center" 

            android:padding="10dp"

            android:text=""/>

    </TableRow>

    <TableRow 

        android:layout_width="fill_parent"

        android:layout_height="wrap_content">

        <TextView 

            android:background="#ff654321"

            android:gravity="center"

            android:padding="10dp" 

            android:text=""/>

        <TextView 

            android:background="#fffedcba"

            android:gravity="center" 

            android:padding="10dp"

            android:text=""/>

    </TableRow>

    <TableRow 

        android:layout_width="fill_parent"

        android:layout_height="wrap_content">

        <TextView android:background="#fffedcba" 

            android:gravity="center"

            android:padding="10dp"

            android:text=""/>

        <TextView  

            android:background="#ff654321"

            android:gravity="center"

            android:padding="10dp" 

            android:text=""/>

        <TextView 

            android:background="#ffffffff" 

            android:gravity="center" 

            android:padding="10dp" 

            android:text=""/>

    </TableRow>

</TableLayout>

文中部分内容参考博文:http://blog.csdn.net/luckkof

Android UI基础之五大布局的更多相关文章

  1. Android UI基础教程 目录

    从csdn下载了这本英文版的书之后,又去京东搞了一个中文目录下来.对照着看. 话说,这本书绝对超值.有money的童鞋看完英文版记得去买中文版的~~ Android UI基础教程完整英文版 pdf+源 ...

  2. Android培训准备资料之五大布局简单介绍

    本篇博客主要简单的给大家介绍一下Android五大布局 (1)LinearLayout(线性布局) (2)RelativeLayout(相对布局) (3)FrameLayout(帧布局) (4)Abs ...

  3. Android UI学习 - FrameLayou和布局优化(viewstub)

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://android.blog.51cto.com/268543/308090 Fram ...

  4. 【Android自学日记】五大布局常用属性

    线性布局(LinearLayout)常用属性: android:orientation="vertical"--决定子类控件的排布方式(vertical垂直:horizontal水 ...

  5. Android UI 学习 自定义的布局 平滑移动 VelocityTracker()

    /**  * Helper for tracking the velocity of touch events, for implementing  * flinging and other such ...

  6. Android UI组件----用相对布局RelativeLayout做一个登陆界面

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...

  7. Android开发-之五大布局

    在html中大家都知道布局是什么意思了,简单来说就是将页面划分模块,比如html中的div.table等.那么Android中也是这样的.Android五大布局让界面更加美化,开发起来也更加方便.当然 ...

  8. Android UI设计规则

    Android UI技巧 1.1 不该做什么 l  不要照搬你在其他平台的UI设计,应该让用户使用感觉是在真正使用一个Android软件,在你的LOGO显示和平台总体观感之间做好平衡 l  不要过度使 ...

  9. eclipse Android 开发基础 Activity 窗体 界面

    eclipse Android 开发基础 新建工程 新建布局layout,new Android Activity就相当于窗体Form. 新建Activity自动生成src下同名的java代码. pu ...

随机推荐

  1. PS网页设计教程XXX——在PS中创建一个漫画书主题网页布局

    作为编码者,美工基础是偏弱的.我们可以参考一些成熟的网页PS教程,提高自身的设计能力.套用一句话,“熟读唐诗三百首,不会作诗也会吟”. 本系列的教程来源于网上的PS教程,都是国外的,全英文的.本人尝试 ...

  2. 关于Redis中的字符串对象

    一.SDS redis中定义Object types有5种 /* Object types */ #define REDIS_STRING 0 #define REDIS_LIST 1 #define ...

  3. 在VS2010中使用Outlook工具栏

    参考资料:微软MSDN.VS2010示例代码 一开始上段子总是能活跃气氛,等哪天我再打开自己的这篇博客,总是能够让自己傻傻的乐一下. 我一女同学,毕业去一大公司应聘,竞争很激烈,最后剩下她和一位女士. ...

  4. STM32启动文件选择说明

    图1. STM32F10xxx标准外设库体系结构先说这个问题,大家都知道,我们在选择使用哪些外围的的时候,是去更改从官方模版中拷贝过来的stm32f10x_conf.h文件的27-48行,把我们要用的 ...

  5. Android 内存分析

    1.使用adb命令查看app内存使用情况 adb shell dumpsys meminfo <package_name> adb shell top | grep <package ...

  6. 形如(function(){}).call()的js语句

    研究新浪微博的自动登陆流程,其中涉及到它的加密算法脚本,其中有一段如下形式的代码: (function(){...}).call(name) 其中红色的....是函数的内部各种实现,name为一个对象 ...

  7. java方法可变参数的写法

    jdk1.5之后出现的,该写法避免了当有多个不同个数的参数方法时,对方法的重载.其实就是数组. package com.shipin; /** * @author QiaoJiafei * @vers ...

  8. jquery模拟下拉框单选框复选Select,Checkbox,Radio

    在项目中,你会发现设计稿中常常会有单选框,复选框,但都不是系统默认的样式,这就可以用jquery来模拟它们:如图所示,实现它们所需要的代码如下: 首先需要引入的代码: <link rel=&qu ...

  9. MySQL数据库学习笔记(五)----MySQL字符串函数、日期时间函数

    一.常见字符串函数: 1.CHAR_LENGTH  获取长度(字符为单位) 2.FORMAT  格式化 3.INSERT  替换的方式插入 4.INSTR  获取位置 5.LEFT/RIGHT  取左 ...

  10. Fidder--实现手机的抓包

    今天闲着没吊事,来写一篇关于怎么抓取Android中的app数据包?工欲行其事,必先利其器,上网google了一下,发现了一款神器:Fiddler,这个貌似是所有软件开发者必备神器呀!这款工具不仅可以 ...