概念


Android程序各式各样,依靠的就是布局,先来看看布局都是怎么来的:

白色部分就是我们经常用的几种布局,主要说说介绍下面五大布局

FrameLayout

AbsoluteLayout

LinearLayout

RelativeLayout

TableLayout

先介绍两种:

线性布局-LinearLayout


在一个方向上对齐所有元素。

可以横着、竖着,也可以嵌套,直接看代码吧

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <!-- vertical 代表垂直布局 --> <TextView
android:layout_width="fill_parent"
android:layout_height="50dip"
android:background="#88AAFF"
android:gravity="center_horizontal"
android:text="ABCDEFG"
android:textSize="14dip" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" > <!-- horizontal 代表横向布局 --> <TextView
android:layout_width="wrap_content"
android:layout_height="50dip"
android:layout_weight="0"
android:background="#22FFFF"
android:text="hello" /> <TextView
android:layout_width="wrap_content"
android:layout_height="50dip"
android:layout_weight="1"
android:background="#FF22FF"
android:text="world" /> <TextView
android:layout_width="wrap_content"
android:layout_height="50dip"
android:layout_weight="2"
android:background="#2222FF"
android:text="ni" /> <TextView
android:layout_width="wrap_content"
android:layout_height="50dip"
android:layout_weight="3"
android:background="#FFFF22"
android:text="hao" />
</LinearLayout> </LinearLayout>

根据上面的布局和程序运行的结果,可以得到如下结论:

1、android:orientation="vertical" 代表垂直布局   horizontal 则代表横向布局

2、android:gravity="center_horizontal" 这一句话使得 ABCDEFG 居中对齐

3、android:layout_weight="0"  这个值决定了占屏幕的百分比的权重

程序运行结果:

看起来对于一般的需求,线性布局就够了。

相对布局-Relative Layout


听说是布局里面功能最强大的,它的存在是为了适应五花八门的屏幕分辨率。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <AnalogClock
android:id="@+id/aclock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<!-- layout_centerInParent="true" 居中显示,将这个控件显示在父窗口的中间位置. --> <DigitalClock
android:id="@+id/dclock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/aclock"
android:layout_below="@id/aclock"
android:layout_marginLeft="40px" /> <!--
layout_alignLeft="@id/aclock" 将控件的左边缘和给定ID控件的左边缘对齐
android:layout_below="@id/aclock" 将控件置于给定ID控件之下
layout_marginLeft 定义的控件左边距为40个dip
--> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/aclock"
android:layout_toLeftOf="@id/dclock"
android:text="当前时间:" /> <!--
android:layout_above="@id/xxx" 将控件置于给定ID控件之上
android:layout_toLeftOf 将控件的右边缘和给定ID控件的左边缘对齐
--> </RelativeLayout>

根据上面注释里面给出来的就是,就可以知道程序运行起来之后是这样样子的:

相对布局果然强大,可以随意布置,下面看一些常用的相对布局使用的属性的含义:

android:layout_above="@id/xxx"            --将控件置于给定ID控件之上
android:layout_below="@id/xxx"            --将控件置于给定ID控件之下
android:layout_toLeftOf="@id/xxx"         --将控件的右边缘和给定ID控件的左边缘对齐
android:layout_toRightOf="@id/xxx"        --将控件的左边缘和给定ID控件的右边缘对齐
android:layout_alignLeft="@id/xxx"        --将控件的左边缘和给定ID控件的左边缘对齐
android:layout_alignTop="@id/xxx"         --将控件的上边缘和给定ID控件的上边缘对齐
android:layout_alignRight="@id/xxx"       --将控件的右边缘和给定ID控件的右边缘对齐
android:layout_alignBottom="@id/xxx"      --将控件的底边缘和给定ID控件的底边缘对齐
android:layout_alignParentLeft="true"     --将控件的左边缘和父控件的左边缘对齐
android:layout_alignParentTop="true"      --将控件的上边缘和父控件的上边缘对齐
android:layout_alignParentRight="true"    --将控件的右边缘和父控件的右边缘对齐
android:layout_alignParentBottom="true"   --将控件的底边缘和父控件的底边缘对齐
android:layout_centerInParent="true"      --将控件置于父控件的中心位置
android:layout_centerHorizontal="true"    --将控件置于水平方向的中心位置
android:layout_centerVertical="true"      --将控件置于垂直方向的中心位置

【Android开发学习笔记】【第七课】五大布局-上的更多相关文章

  1. 【Android开发学习笔记之一】5大布局方式详解

    Android中常用的5大布局方式有以下几种: 线性布局(LinearLayout):按照垂直或者水平方向布局的组件. 帧布局(FrameLayout):组件从屏幕左上方布局组件. 表格布局(Tabl ...

  2. android开发学习笔记000

    使用书籍:<疯狂android讲义>——李刚著,2011年7月出版 虽然现在已2014,可我挑来跳去,还是以这本书开始我的android之旅吧. “疯狂源自梦想,技术成就辉煌.” 让我这个 ...

  3. Android开发学习路线的七个阶段和步骤

    Android开发学习路线的七个阶段和步骤           Android学习参考路线     第一阶段:Java面向对象编程 1.Java基本数据类型与表达式,分支循环. 2.String和St ...

  4. 【Android开发学习笔记】【第八课】五大布局-下

    概念 五大布局上一篇文章已经介绍了 LinearLayout RelativeLayout 这一篇我们介绍剩下的三种布局 FrameLayout 五种布局中最佳单的一种布局.在这个布局在整个界面被当成 ...

  5. 【转】Android开发学习笔记:5大布局方式详解

    Android中常用的5大布局方式有以下几种: 线性布局(LinearLayout):按照垂直或者水平方向布局的组件. 帧布局(FrameLayout):组件从屏幕左上方布局组件. 表格布局(Tabl ...

  6. 【前端】移动端Web开发学习笔记【2】 & flex布局

    上一篇:移动端Web开发学习笔记[1] meta标签 width设置的是layout viewport 的宽度 initial-scale=1.0 自带 width=device-width 最佳实践 ...

  7. android开发学习笔记系列(2)-android应用界面编程

    前言 本篇博客将会简要介绍andriod开发过程中的一些界面元素和编程的实现,我将大家走进安卓的XML世界,当然可能会涉及到java代码,当然本文主要是介绍XML文件的界面布局. 那么我们的XML存在 ...

  8. android开发学习笔记系列(1)-android起航

    前言 在学习安卓的过程中,我觉得非常有必要将自己所学的东西进行整理,因为每每当我知道我应该是如何去实现功能的时候,有许多细节问题我总是会遗漏,因此我也萌生了写一系列博客来描述自己学习的路线,让我的an ...

  9. 【转】Android开发学习笔记(一)——初识Android

    对于一名程序员来说,“自顶向下”虽然是一种最普通不过的分析问题和解决问题的方式,但其却是简单且较为有效的一种.所以,将其应用到Android的学习中来,不至于将自己的冲动演变为一种盲目和不知所措. 根 ...

随机推荐

  1. js:语言精髓笔记7----原型继承

    面向对象有三个基本特性:封装,继承,多态:如果都满足的话称为面向对象语言:而部分满足则称为基于对象语言: 数据类型实现模型描述: JavaScript对象模型: 构造过程:函数->构造器 构造器 ...

  2. html和text的区别

    1.html <div id="divShow"><b><i>Write Less Do More</i></b>< ...

  3. ISODATA算法

    ISODATA算法是在k-均值算法的基础上,增加对聚类结果的'合并'和'分裂'两个操作,并 设定算法运行控制参数的一种聚类算法. 全称:Iterative Selforganizing Data An ...

  4. BZOJ4297 : [PA2015]Rozstaw szyn

    每个点的最优取值范围是一个区间,将叶子一层层剥去,得到一棵有根树,父亲的取值范围由儿子推得,时间复杂度$O(n\log n)$. #include<cstdio> #include< ...

  5. [译]JavaScript:函数的作用域链

    原文:http://blogs.msdn.com/b/jscript/archive/2007/07/26/scope-chain-of-jscript-functions.aspx 在JavaScr ...

  6. Uiautomator打包使用第三方库,报错的解决方案

    问题引源: 在做自动化过程中,想在用例执行完毕后,自动生成该用例测试报告: 报告定义为Excel格式文件,且在用例执行过程中生成. 所以我在Uiautomator工程中引用了jxl.jar,用以处理E ...

  7. Qt5.4 VS2010 Additional Dependancies

    Go to Linker -> General -> Additional LIbrary Directories: qtmaind.libQt5Cored.libQt5Guid.libQ ...

  8. hdu 1735 字数统计

    这道题是到贪心的题目,首先用ans记录下所有的0的个数,然后尽量去掉更多的0,剩下的0的个数就是最少的字数.首先想到最后一行的0的个数可以减掉,然后就是m行开头的两个0可以减掉.然后思考最多还可以减掉 ...

  9. MS14-068 privilege escalation PoC: 可以让任何域内用户提升为域管理员

    https://github.com/bidord/pykek ms14-068.py Exploits MS14-680 vulnerability on an un-patched domain ...

  10. Ctrl+Shift+F12切换最大化编辑器

    常用快捷键(keymaps:Default情况下) Esc键编辑器(从工具窗口) F1帮助千万别按,很卡! F2(Shift+F2)下/上高亮错误或警告快速定位 F3向下查找关键字出现位置 F4查找变 ...