概述:

  布局(Layout)的概念是针对Activity的,Activity就是布满整个Android设备的窗体或者悬浮于其它窗体上的交互界面。在一个应用程序中通常由多个Activity构成。每一个须要显示的Activity都须要在AndroidManifest.xml文件之中声明。

  通常情况下,能够使用两种方式来创建UI组件,一种方式是使用XML方式来配置UI组件的相关属性。然后装载这些UI组件,这也是最经常使用的方式。可是有些特殊情况下,须要动态生成UI组件。则须要使用另外一种方式,全然使用Java代码来创建UI组件。

  XML布局文件是Android系统中定义的Layout的经常使用方式,全部布局文件必须包括在res/layout文件夹中。且必须符合Java的命名规范。

当在res/layout文件夹下新增了布局文件之后。R.java文件会自己主动收录该布局资源,Java代码可通过setContentView方法在Activity中显示该Layout。

  setContentView(R.layout.<资源名称>)。

在布局文件里能够指定UI组件的android:id属性,该属性的属性值代表该组件的唯一标识。通过Activity.findViewById()訪问,而且findViewById()必须在setContentView载入xml文件之后使用,否则会抛出异常。

  

  findViewById(R.id.)

  Android应用的绝大部分UI组件都放在android.widget包及其子包、android.view包及其子包中。Android应用的全部UI组件都继承了View类。

View类另一个重要的子类:ViewGroup,ViewGroup类是全部布局管理器的父类。

  ViewGroup容器控制其子组件的分布依赖于ViewGroup.LayoutParams、ViewGroup.MarginLayoutParams两个内部类。

  ViewGroup.LayoutParams提供两个XML属性设定组件的大小。

  • android:layout_height:指定该子组件的基本高度;

  • android:layout_width:指定该子组件的基本宽度。

这两个属性有三个基本值。这两个属性有三个特定的值:

  • fill_parent:指定组件的高度、宽度与父容器组件的一样。

  • match_parent:与fill_parent一样,Android2.2開始推荐使用。

  • warp_content:内容包裹。

ViewGroup.MarginLayoutParams用于控制子组件周围的页边距。

  • android:layout_marginBottom(下边距);

  • android:layout_marginLeft(左边距);

  • android:layout_marginRight(右边距):

  • layout_marginTop(上边距)

  对于View的尺寸。android提供了三种单位供选择使用:

  • px:像素。

  • dp:dpi。表示屏幕实际的像素。

  • sp:与scale无关的像素。与dp类似。

  尺寸单位选择的技巧:假设设置长度、高度等属性时能够使用dp或sp。可是假设设置字体,须要使用px。假设使用dp或sp,系统会依据屏幕密度的变化进行转换。

  为了适应各种界面风格,Android提供了六种布局规范。利用这六种布局,基本上能够在设备上随心所欲的摆放不论什么UI组件,这六种布局各自是:

  • RelativeLayout(相对布局)

  • LinearLayout(线性布局)

  • TableLayout(表格布局)

  • GridLayout(网格布局)

  • FrameLayout(帧布局)
  • AbsoluteLayout(绝对布局)

布局类型:

相对布局(RelativeLayout):

RelativeLayout。其内子组件的位置总是相对兄弟UI组件、父亲容器来决定的。

比方UI组件A相对于UI组件B的位置进行定位,那么UI组件B须要在UI组件A之前定义。

 相对布局用到的主要属性:

  • android:layout_below:在某元素的下方。

  • android:layout_above:在某元素的上方。
  • android:layout_toLeftOf:在某元素的左边。
  • android:layout_toRightOf:在某元素的右边。

  • android:layout_alignXxx:控制与某元素的边界对其方式。

这个比較好玩,在元素的位置的时候,使用相对位置,能够相对其它元素,也能够相对这个布局,就像我说:我如今站在pawa和 tempest的中间;或者说,我站在队伍的中间。前者就是相对其它元素来定义位置,后者是相对整个布局来定义位置。

<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="260dp"
android:text="Button1"/> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/button1"
android:layout_marginBottom="100dp"
android:layout_toRightOf="@id/button1"
android:text="Button2"/> </RelativeLayout>

线性布局(LinearLayout):

LinearLayout是最经常使用的布局方式,它会将容器里的UI组件一个一个挨着排列起来。

可是LinearLayout不会换行。当UI组件超出屏幕之后。则不会被显示出来。

LinearLayout有两个重要的XML属性:

androidgravity(对齐方式)

android:orientation(排列方式)

  android:orientation(排列方式),设定了LinearLayout中包括的UI组件的排列方式。有两个选项vertical(竖向)、horizontal(横向,默认值)



  android:gravity(对齐方式),设定LinearLayout中包括UI组件的对齐方式。其选项非常多,经常使用上(top)、下(bottom)、左(left)、右(right)。

这样的布局比較经常使用,也比較简单,就是每一个元素占一行,当然也可能声明为横向排放,也就是每一个元素占一列。

<?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"> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"/> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"/> <Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3"/>
</LinearLayout>

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

表格布局(TableLayout):

表格布局,採用行、列的形式来管理UI组件,TableLayout通过TableRow、其它UI组件来控制表格的行数和列数。

  每次向TableLayout中加入一个TableRow。该TableRow就是一个表格行,TableRow也是容器,因此它也能够不断加入其它组件,没加入一个子组件。该表格就添加一列。假设直接向TableLayout中加入组件。那么这个组件将直接占用一行。

  TableLayout支持的XML属性

  • android:collapseColumns:设置须要被隐藏的列序号。
  • android:shrinkColumns:设置须要被收缩的列序号。
  • android:stretchColumns:设置须要被拉伸的列序号。

  注意:TableLayout中所谓的序列号是从0開始计算的。

<?

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="2"> <TableRow>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:text="Button1"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:text="Button2"/>
</TableRow> <TableRow>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:text="Button3"/>
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:text="Button4"/>
</TableRow> <TableRow>
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:text="Button5"/>
</TableRow><h3><span style="font-family:宋体;"><span style="font-size:12px;">androidgravity(对齐方式)</span></span></h3><h3><span style="font-family:宋体;"><span style="font-size:12px;">android:orientation(排列方式)</span></span></h3> </TableLayout>

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

网格布局(GridLayout):

说明:网格布局是4.0之后加入的布局。跟TableLayout有点像,但更加好用,它把容器分为一个rows*columns的网格,每一个网格都是一个组件位,但是通过设置让组件位占领多行/列。

GridLayout有两类须要关注的属性:

1 GridLayout本身的属性

说明:这类属性是针对GridLayout自身来设置的。主要是对行和列。以及对其方式的设置。

属性表例如以下:

属性 相应方法 属性说明
android:columnCount setColumCount(int) 设置布局的最大列数
android:rowCount setRowCount(int) 设置布局的最大行数
android:alignmentMode setAilgnmentMode(int) 设置布局的对其方式(alignBounds: 对齐子视图边界;alignMargins: 对齐子视图边距;)
android:columnOrderPeserved setColumOrderPreserved(boolean) 设置容器是否保留列序号
android:rowOrderPeserved setRowOrderPeserved(boolean) 设置容器是否保留行序号
android:useDefaultMargins setUseDefaultMargins(boolean) 设置容器是否使用默认的页边距

2 针对容器内的子组件的属性

说明:这类属性是针对GridLayout中的子组件设置的,能够设置组件在网格中的大小和摆放方式。

属性表例如以下:

属性 相应方法 属性说明
android:layout_Gravity setGravity(int) 设置组件怎样占领其所属网格的空间
android:layout_column   设置组件在容器的第几列
android:layout_row   设置组件在容器的第几行
android:layout_columnSpan   设置组件占领了几列
android:layout_rowSpan   设置组件占领了几行
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:columnCount="4"
android:orientation="horizontal"> <Button
android:layout_column="3"
android:text="/"/>
<Button android:text="1"/>
<Button android:text="2"/>
<Button android:text="3"/>
<Button android:text="*"/>
<Button android:text="4"/>
<Button android:text="5"/>
<Button android:text="6"/>
<Button android:text="-"/>
<Button android:text="7"/>
<Button android:text="8"/>
<Button android:text="9"/>
<Button
android:layout_gravity="fill"
android:layout_rowSpan="3"
android:text="+"/>
<Button
android:layout_columnSpan="2"
android:layout_gravity="fill"
android:text="0"/>
<Button android:text="00"/>
<Button
android:layout_columnSpan="3"
android:layout_gravity="fill"
android:text="="/> </GridLayout>

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

帧布局(FrameLayout):

帧布局是最简单的布局方式。全部加入到这个布局中的视图都是以层叠的方式显示,而且后声明的遮挡先声明的控件。

帧布局容器为每一个增加当中的组件创建一个空白的区域(称为一帧),全部每一个子组件占领一帧,这些帧都会依据gravity属性运行自己主动对齐。

帧布局方式。说帧不太easy理解,能够说成是层布局方式,也就是说,在它内部的元素。是一层一层的叠加在一起的。假设用过Photoshop,或者 Flash,这里面的层的概念是和他们一致的。

假设最上层的元素是不透明的,而且比以下的元素尺寸要大,那么将看不到以下的元素,仅仅能看到顶层元素。

这些 层的顺序是:最新声明的放到最前面。能够这样理解,Android按文件的书写顺序来组织这个布局,先声明的放在第一层。再声明的放到第二层,…,最后声 明的放在最顶层。

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

>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <Button
android:id="@+id/button1"
android:layout_width="294dp"
android:layout_height="294dp"
android:text="Button1"/>
<Button
android:id="@+id/button2"
android:layout_width="218dp"
android:layout_height="214dp"
android:text="Button2"/>
<Button
android:id="@+id/button3"
android:layout_width="156dp"
android:layout_height="143dp"
android:text="Button3"/>
</FrameLayout>

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

绝对布局(AbsoluteLayout):

对于AbsoluteLayout,android不提供不论什么布局控制,而是由开发者自己通过X坐标、Y坐标来控制组件的位置。



  在AbsoluteLayout中,每一个子组件都须要通过两个XML属性来确定坐标:layout_x:指定该子组件的X坐标。layout_y:指定该子组件的Y坐标。



  由于此布局比較繁琐。并且在不同的屏幕上显示效果差距比較大,所以一般不推荐使用。

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="50dp"
android:layout_y="50dp"
android:text="Button1"/> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="200dp"
android:layout_y="150dp"
android:text="Button2"/>
</AbsoluteLayout>

布局案例——用户注冊:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/regist_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="22dp"
android:orientation="horizontal"> <TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:gravity="right"
android:paddingRight="5dp"
android:text="用户名:"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入您的用户名"
android:textSize="14dp"/> </LinearLayout>
<LinearLayout
android:id="@+id/regist_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/regist_username"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:orientation="horizontal"> <TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:gravity="right"
android:paddingRight="5dp"
android:text="密 码:"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入您的密码"
android:inputType="textPassword"
android:textSize="14dp"/>
</LinearLayout>
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/regist_password"
android:layout_marginLeft="30dp"
android:contentDescription="性别"
android:orientation="horizontal"> <RadioButton
android:id="@+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="男"/>
<RadioButton
android:id="@+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
</RadioGroup> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/radioGroup"
android:layout_centerHorizontal="true"
android:layout_marginTop="36dp"
android:text="注冊"/>
</RelativeLayout>

Android笔记——UI开发的更多相关文章

  1. android笔记1——开发环境的搭建

    Long Long ago...已经成为了历史,我还是要说出一个真相:早年前,那时候,android还不被大众所认知的时候,当然开发人员也没不像如今那样趋于饱和状态.一位大牛前辈,也是我的学长,那时候 ...

  2. Android:日常学习笔记(8)———探究UI开发(5)

    Android:日常学习笔记(8)———探究UI开发(5) ListView控件的使用 ListView概述 A view that shows items in a vertically scrol ...

  3. Android:日常学习笔记(7)———探究UI开发(4)

    Android:日常学习笔记(7)———探究UI开发(4) UI概述  View 和 ViewGrou Android 应用中的所有用户界面元素都是使用 View 和 ViewGroup 对象构建而成 ...

  4. Android:日常学习笔记(8)———探究UI开发(3)

    Android:日常学习笔记(8)———探究UI开发(3) 详解四种基本布局 前言 布局定义用户界面的视觉结构,如Activity或应用小部件的 UI.您可以通过两种方式声明布局: 在 XML 中声明 ...

  5. Android:日常学习笔记(8)———探究UI开发(2)

    Android:日常学习笔记(8)———探究UI开发(2) 对话框 说明: 对话框是提示用户作出决定或输入额外信息的小窗口. 对话框不会填充屏幕,通常用于需要用户采取行动才能继续执行的模式事件. 提示 ...

  6. Android:日常学习笔记(7)———探究UI开发(1)

    Android:日常学习笔记(7)———探究UI开发(1) 常用控件的使用方法 TextView 说明:TextView是安卓中最为简单的一个控件,常用来在界面上显示一段文本信息. 代码: <T ...

  7. 【转】Pro Android学习笔记(十三):用户界面和控制(1):UI开发

    目录(?)[-] UI开发 方式一通过XML文件 方式二通过代码 方式三XML代码 UI开发 先理清一些UI概念: view.widget.control:这三个名词其实没有什么区别,都是一个UI元素 ...

  8. Android 高级UI设计笔记07:RecyclerView 的详解

    1. 使用RecyclerView       在 Android 应用程序中列表是一个非常重要的控件,适用场合非常多,如新闻列表.应用列表.消息列表等等,但是从Android 一出生到现在并没有非常 ...

  9. Android移动APP开发笔记——最新版Cordova 5.3.1(PhoneGap)搭建开发环境

    引言 简单介绍一下Cordova的来历,Cordova的前身叫PhoneGap,自被Adobe收购后交由Apache管理,并将其核心功能开源改名为Cordova.它能让你使用HTML5轻松调用本地AP ...

随机推荐

  1. How To Install Linux, nginx, MySQL, PHP (LEMP) stack on CentOS 6

    About Lemp LEMP stack is a group of open source software to get web servers up and running. The acro ...

  2. centos 7 mini版中安装Python3.x

    首先了解几句Linux命令是必须的.例如 ls, vi, wget, rm, mv, cd, su, sudo, chmod, tar等等一些常用的语句命令是有必要知道它的用法的. 安装Python3 ...

  3. 转:西部数据NAS设备hack

    通过该文学习一下常见硬件web漏洞.重点关注一下几个方面: 1.登录验证代码: 2.文件上传代码: 3.system/exec/popen等是否存在注入可能: 4.调用二进制文件: 5.未登陆可以访问 ...

  4. Linux下python版本的升级步骤

    1.先下载,你要升级的python版本(我升级的是python3.3.0) 可使用系统自带下载工具wget下载: wget http://www.python.org/ftp/python/3.3.0 ...

  5. CodeForces 143C Help Farmer

    暴力枚举. 枚举最小的那个数字,不会超过$1000$,剩下的两个数字根号的效率枚举一下即可. #include<bits/stdc++.h> using namespace std; lo ...

  6. SpringBoot+Mybatis整合实例

    前言 大家都知道springboot有几大特点:能创建独立的Spring应用程序:能嵌入Tomcat,无需部署WAR文件:简化Maven配置:自动配置Spring等等.这里整合mybatis,创建一个 ...

  7. FZOJ 2245 动态树(离散+离线+ 树状数组)

    Problem 2245 动态树 Accept: 17    Submit: 82Time Limit: 3000 mSec    Memory Limit : 65536 KB  Problem D ...

  8. 「2018山东一轮集训」 Tree

    为什么出题人这么毒瘤啊??!!一个分块还要带log的题非要出成n<=2*1e5....... 为了卡过最后两个点我做了无数常数优化,包括但不限于:把所有线段树改成 存差分的树状数组:把树剖求LC ...

  9. 【莫比乌斯反演+容斥】BZOJ2301-[HAOI2011]Problem b(成为权限狗的第一题纪念!)

    [题目大意] 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. [思路] “怎么又是你系列……”思路 ...

  10. 给lnmp一键包中的nginx安装openresty的lua扩展

    lnmp一键包(https://lnmp.org)本人在使用之后发现确实好用,能帮助我们快速搭建起lnmp.lamp和lnmpa的web生产环境,因此推荐大家可以多试试.但有的朋友可能需要使用open ...