概念


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. node基础 --工具

    npm  //Node.js Package Manager 查询版本号:npm --version/npm version 查询模块:npm search <name> 查看模块相关信息 ...

  2. CGOS461 [网络流24题] 餐巾(最小费用最大流)

    题目这么说的: 一个餐厅在相继的N天里,第i天需要Ri块餐巾(i=l,2,…,N).餐厅可以从三种途径获得餐巾. 购买新的餐巾,每块需p分: 把用过的餐巾送到快洗部,洗一块需m天,费用需f分(f< ...

  3. MP3/视频播放

    简单的视频.MP3播放 <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equ ...

  4. !cocos2d ccdictionary->retain()的问题

    我再a类当中生命了一个dict,将它带入到b类当中,但没有在b类初始化时retain,于是在update当中找不到了.啃爹不.记得retain()

  5. tiledmap2

    1 1.1 将tiledmap 保存为xml格式 1.2 在unity当中设置摄像机为"Orthogonal", 1.3 拖拽tiledmap prefab从project视图到H ...

  6. 解决javax.imageio.IIOException: Can't create output stream!

    解决javax.imageio.IIOException: Can't create output stream! javax.imageio.ImageIO.write(image, "J ...

  7. Qt 5.4 OpenCV 2.4.11 Win 32 Configuration

    Download CMake 2.8.2 Download OpenCV 2.4.11 Download Qt 5.4 Highly improtant note: The installation ...

  8. hdu 4310 Hero

    这道题是道算是一道很简单的贪心题了,但是要注意排序的依据,这道题是按照dps/hp的从大到小排序的,然后计算总的sumhp即可. #include"iostream" #inclu ...

  9. javaWeb应用部署结构浅析

    要成功部署一个Web应用,则必须遵循以下标准(参考)目录结构. 2.目录说明 1)WEB-INF目录:必须直接放在Web应用上下文之下(即一级目录). 2)class目录:必须直接放在WEB-INF目 ...

  10. POJ 1144 Network(Tarjan求割点)

    Network Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12707   Accepted: 5835 Descript ...