概念


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"      --将控件置于垂直方向的中心位置

最新文章

  1. yii2 伪静态配置
  2. Filter
  3. 【BZOJ 2693】jzptab
  4. ubuntu如何开启root,如何启用Ubuntu中root帐号
  5. jQuery 删除或是清空某个HTML元素示例
  6. bootstrap-js(六)弹出框
  7. json.net 比jsonIgnore 更好的方法 修改源码
  8. css中所有的选择器
  9. (四) 虚拟摄像头vivi体验
  10. 异常之Tomcat8
  11. 使用R的注意事项
  12. Java SE HashMap的底层实现
  13. ubuntu12.04下安装Apache+PHP+MySQL
  14. Spring(十六):泛型依赖注入
  15. 双系统linux+win之血的教训
  16. mysql 数据备份及数据迁移
  17. 使用delphi 开发多层应用(十九) ios通过soap 访问kbmmw服务器
  18. 《精通Python设计模式》学习结构型之享元模式
  19. 沈阳网络赛D-Made In Heaven【k短路】【模板】
  20. Ant+jmeter+jenkins搭建测试的持续集成

热门文章

  1. mvc-1mvc和类(1)
  2. POJ3493 Largest Submatrix of All 1’s(单调栈)
  3. POJ1845 Sumdiv(求所有因数和+矩阵快速幂)
  4. BZOJ3249 : [ioi2013]game
  5. [转]C++设计模式:Builder模式
  6. [译]JavaScript:函数的作用域链
  7. 搭建你的第一个Django应用程序
  8. c++ map 的使用
  9. 【Linux】linux常用基本命令(转)
  10. Odoo 外协加工产品的实现