【Android开发学习笔记】【第七课】五大布局-上
概念
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开发学习笔记】【第七课】五大布局-上的更多相关文章
- 【Android开发学习笔记之一】5大布局方式详解
Android中常用的5大布局方式有以下几种: 线性布局(LinearLayout):按照垂直或者水平方向布局的组件. 帧布局(FrameLayout):组件从屏幕左上方布局组件. 表格布局(Tabl ...
- android开发学习笔记000
使用书籍:<疯狂android讲义>——李刚著,2011年7月出版 虽然现在已2014,可我挑来跳去,还是以这本书开始我的android之旅吧. “疯狂源自梦想,技术成就辉煌.” 让我这个 ...
- Android开发学习路线的七个阶段和步骤
Android开发学习路线的七个阶段和步骤 Android学习参考路线 第一阶段:Java面向对象编程 1.Java基本数据类型与表达式,分支循环. 2.String和St ...
- 【Android开发学习笔记】【第八课】五大布局-下
概念 五大布局上一篇文章已经介绍了 LinearLayout RelativeLayout 这一篇我们介绍剩下的三种布局 FrameLayout 五种布局中最佳单的一种布局.在这个布局在整个界面被当成 ...
- 【转】Android开发学习笔记:5大布局方式详解
Android中常用的5大布局方式有以下几种: 线性布局(LinearLayout):按照垂直或者水平方向布局的组件. 帧布局(FrameLayout):组件从屏幕左上方布局组件. 表格布局(Tabl ...
- 【前端】移动端Web开发学习笔记【2】 & flex布局
上一篇:移动端Web开发学习笔记[1] meta标签 width设置的是layout viewport 的宽度 initial-scale=1.0 自带 width=device-width 最佳实践 ...
- android开发学习笔记系列(2)-android应用界面编程
前言 本篇博客将会简要介绍andriod开发过程中的一些界面元素和编程的实现,我将大家走进安卓的XML世界,当然可能会涉及到java代码,当然本文主要是介绍XML文件的界面布局. 那么我们的XML存在 ...
- android开发学习笔记系列(1)-android起航
前言 在学习安卓的过程中,我觉得非常有必要将自己所学的东西进行整理,因为每每当我知道我应该是如何去实现功能的时候,有许多细节问题我总是会遗漏,因此我也萌生了写一系列博客来描述自己学习的路线,让我的an ...
- 【转】Android开发学习笔记(一)——初识Android
对于一名程序员来说,“自顶向下”虽然是一种最普通不过的分析问题和解决问题的方式,但其却是简单且较为有效的一种.所以,将其应用到Android的学习中来,不至于将自己的冲动演变为一种盲目和不知所措. 根 ...
随机推荐
- [LintCode] Continuous Subarray Sum II
Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Y ...
- 暴力/set Codeforces Round #291 (Div. 2) C. Watto and Mechanism
题目传送门 /* set的二分查找 如果数据规模小的话可以用O(n^2)的暴力想法 否则就只好一个一个的换(a, b, c),在set容器找相匹配的 */ #include <cstdio> ...
- ZOJ 3913 Bob wants to pour water ZOJ Monthly, October 2015 - H
Bob wants to pour water Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge There i ...
- BZOJ1770 : [Usaco2009 Nov]lights 燈
设$f[i]$表示$i$点按下开关后会影响到的点的集合,用二进制表示. 不妨设$n$为偶数,令$m=\frac{n}{2}$,对于前一半暴力$2^m$搜索所有方案,用map维护每种集合的最小代价. 对 ...
- BZOJ3833 : [Poi2014]Solar lamps
首先旋转坐标系,将范围表示成矩形或者射线 如果范围是一条线,则将灯按y坐标排序,y坐标相同的按x坐标排序, 对于y相同的灯,f[i]=min(i,它前面灯发光时刻的第k[i]小值), 线段树维护,$O ...
- XCOJ 1102 (树形DP+背包)
题目链接: http://xcacm.hfut.edu.cn/oj/problem.php?id=1102 题目大意:树上取点.父亲出现了,其儿子包括孙子...都不能出现.给定预算,问最大值. 解题思 ...
- Quartz Cron 表达式
Cron 表达式包括以下 7 个字段 格式: [秒] [分] [小时] [日] [月] [周] [年] 说明 是否必填 允许填写的值 允许的通配符 秒 是 - , - * / 分 是 - , - * ...
- Lazy Load, 延迟加载图片的 jQuery 插件【备忘】
http://www.neoease.com/lazy-load-jquery-plugin-delay-load-image/ jQuery Unveil – 另一款非常轻量的延迟加载插件 http ...
- Java/Js下使用正则表达式匹配嵌套Html标签
转自:http://www.jb51.net/article/24422.htm 以前写过一篇文章讲解如何使用正则表达式完美解决Html嵌套标签的匹配问题(使用正则表达式匹配嵌套Html标签),但是里 ...
- 关于HTML条件注释你可能不知道的一些事儿
最近经常看到类似这样的HTML代码片段,很多前端开发人员应该都熟悉: 1 <!--[if lt IE 7]> <html class="ie6"> ...