首先介绍常用布局类

  • FrameLayout 最简单的布局管理器。

  这个布局管理类有几个特性:

  1. 添加组件默认在左上角的。
  2. 如果添加多个组件会叠加到一起,并且都在左上角。(可以通过一gravity属性改变叠加情况)
  3. 后添加的组件在上层。
  • LinearLayout LinearLayout通过垂直方向和水平方面进行布局的,LinearLayout允许每一个视图都有一个weight属性。
  • RelativeLayout RelativeLayout是相对于其他的组件和屏幕边缘布局的管理类。
  • GridLayout GridLayout是Android4.0引入的概念,网格的布局方式,及其灵活,可以减少嵌套布局方式。

1.定义布局

定义一个简单的XML布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:text="xxx"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

定义了一个线性布局方式,方向是垂直方向的(通过android:orientation="vertical"属性控制)

这里有三个常量我们将进行详细讲解fill_parent,match_parent,wrap_content。

  • fill_parent 填满父类的View,Fragment,Activity的空间。
  • wrap_content 把视图大小设置为显示内容的最小尺寸。
  • match_parent 从Android 2.2开始fill_parent改名为match_parent。

用java代码实现布局

  LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL); Button button = new Button(this);
button.setText("xxx"); int height = LinearLayout.LayoutParams.MATCH_PARENT;
int weight = LinearLayout.LayoutParams.WRAP_CONTENT; layout.addView(button, new LinearLayout.LayoutParams(height, weight));
setContentView(layout);

这个代码也比较简单,不做详细介绍了。

2.使用布局创建和设备无关的UI

1.LinearLayout(线性布局)

线性布局是最简单的布局之一,简单但是缺少了灵活性。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="xx1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="xx2"
/>

  <!--被嵌套的垂直LinearLayout布局-->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="xx3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="xx4"/>
</LinearLayout>
</LinearLayout>

效果下如图,这里有嵌套布局,一个水平布局的LinearLayout里面嵌套了一个竖直的LinearLayout布局方式。

2.RelativeLayout(相对布局)

RelativeLayout布局非常的灵活,主要针对边框和其他组件进行布局。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/buttonBar"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"> <Button
android:text="xx1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="xx2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout> <Button
android:text="xx3"
android:layout_above="@+id/buttonBar"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</RelativeLayout>

效果图如下

讲几个属性,这个属性对相对布局非常重要

  • android:layout_centerHrizontal  水平居中
  • android:layout_centerVertical 竖直居中
  • android:layout_centerInparent 相对于父类完全居中
  • android:layout_alignParentBottom 贴紧父元素的下边缘
  • android:layout_alignParentLeft 贴紧父元素的左边缘
  • android:layout_alignParentRight 贴紧父元素的右边缘
  • android:layout_alignParentTop 贴紧父元素的上边缘
  • android:layout_alignWithParentIfMissing 如果对应的兄弟元素找不到的话就以父元素做参照物

来个小的总结

android:layout_centerInparent 相当于layout_centerHrizontal和layout_centerVertical一起用是一个效果,如果同时设置android:layout_alignParentBottom和android:layout_alignParentTop会把控件拉长和父类一样,同样道理android:layout_alignParentLeft和android:layout_alignParentRight也一样拉长控件。

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

来个小总结

如果同时设置了android:layout_below和android:layout_above并且id指向的是同一个控件,这是控件会消失不见。如果同时设置了android:layout_below和android:layout_above指定不同的控件会把控件拉长。

  • android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐
  • android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐
  • android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐
  • android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐

来个小总结

同样这样的布局方式也会有拉长的情况。

  • android:layout_marginBottom 离某元素底边缘的距离
  • android:layout_marginLeft 离某元素左边缘的距离
  • android:layout_marginRight 离某元素右边缘的距离
  • android:layout_marginTop 离某元素上边缘的距离

3.GridLayout(网格布局)

网格布局在Android3.0(API level 11)导入,是所有布局管理器中最灵活的一种。

<?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:alignmentMode="alignMargins"
android:orientation="horizontal"
android:columnOrderPreserved="true"
android:rowCount="3"
android:rowOrderPreserved="true"
android:columnCount="4"> <Button android:id="@+id/xx1" android:text="1" />
<Button android:text="2" android:layout_column="0" android:layout_row="2"/>
<Button android:text="3" android:layout_rowSpan="3" android:layout_gravity="fill"/>
<Button android:text="4"/>
<Button android:text="5"/>
<Button android:text="6"/>
<Button android:text="7"/>
<Button android:text="8"/>
<Button android:text="9"/>
<Button android:text="10"/> </GridLayout>

GridLayout下的属性

  • android:orientation  在GridLayout的根目录下,有两个属性horizontal和vertical顾名思义就是水平和竖直布局。
  • android:rowCount 网格的行数
  • android:columnCount 网格的列数

包含控件属性

  • android:layout_columnSpan 水平合并单元格
  • android:layout_rowSpan 竖直合并单元格
  • android:layout_gravity 有很多属性:fill,bottom 等等。

                  作者   Young-Ken(微博)

                  审阅者 Cindy-Leee(微博)转载请注明 http://www.cnblogs.com/youngKen/p/4831953.html

                         

Android UI -- 布局介绍(布局包括FrameLayout, LinearLayout, RelativeLayout, GridLayout)的更多相关文章

  1. 【转】Android UI 五种布局

    在一个Android应用中,Layout是开发中的一个很重要环节,Layout是组成UI不可缺少的一部分. ## Android UI 核心类 在Android应用构建UI的方法有以下几种: 单纯使用 ...

  2. Android UI组件:布局管理器

    为了更好的管理Android应用的用户界面中的组件,Android提供了布局管理器.通过使用布局管理器,Android应用的图形用户界面具有良好的平台无关性.通常,推荐使用布局管理器来管理组件的分布. ...

  3. Android UI测量、布局、绘制过程探究

    在上一篇博客<Android中Activity启动过程探究>中,已经从ActivityThread.main()开始,一路摸索到ViewRootImpl.performTraversals ...

  4. Android UI 使用HTML布局(直接打开server网页)

    非常多时候我们用HTML布局会更方便直接,记录一下. 我如今主要是直接调用server的网页(实际上是jsp的,仅仅是返回的是html).所以须要联网,第一步加入权限. <uses-permis ...

  5. android UI进阶之用【转】

    android UI进阶之用ViewPager实现欢迎引导页面 摘要: ViewPager需要android-support-v4.jar这个包的支持,来自google提供的一个附加包.大家搜下即可. ...

  6. Android开发1:基本UI界面设计——布局和组件

    前言 啦啦啦~本学期要开始学习Android开发啦~ 博主在开始学习前是完完全全的小白,只有在平时完成老师要求的实验的过程中一步一步学习~从此篇博文起,博主将开始发布Android开发有关的博文,希望 ...

  7. 【详细】Android入门到放弃篇-YES OR NO-》各种UI组件,布局管理器,单元Activity

    问:达叔,你放弃了吗? 答:不,放弃是不可能的,丢了Android,你会心疼吗?如果别人把你丢掉,你是痛苦呢?还是痛苦呢?~ 引导语 有人说,爱上一个人是痛苦的,有人说,喜欢一个人是幸福的. 人与人之 ...

  8. Android五大布局介绍&属性设置大全

    前言 在进行Android开发中,常常需要用到各种布局来进行UI的绘制,今天我们就来讲下Android开发中最常用的五大布局介绍和相关属性的设置. 目录 Android五大布局介绍&属性设置. ...

  9. 让我们创建屏幕- Android UI布局和控件

    下载LifeCycleTest.zip - 278.9 KB 下载ViewAndLayoutLessons_-_Base.zip - 1.2 MB 下载ViewAndLayoutLessons_-_C ...

随机推荐

  1. 阿里云Ubuntu服务器安装java环境

    一.下载jdk wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-secureb ...

  2. [搜片神器]BT管理程序数据库速度调试优化问题

    DHT抓取程序开源地址:https://github.com/h31h31/H31DHTDEMO 数据处理程序开源地址:https://github.com/h31h31/H31DHTMgr 谢谢园子 ...

  3. 1025: [SCOI2009]游戏 - BZOJ

    Description windy学会了一种游戏.对于1到N这N个数字,都有唯一且不同的1到N的数字与之对应.最开始windy把数字按顺序1,2,3,……,N写一排在纸上.然后再在这一排下面写上它们对 ...

  4. centos7安装mplayer以及出现的各种问题

    首先,centos7默认的视频播放器基本不能用,这里我们选择mplayer作为视频播放器. 安装的过程,痛并快乐着....... 首先我们去mplayer的官网下载需要的文件,http://www.m ...

  5. centos最小安装 setuptools安装

    centos运行不了setup?那安装setuptool吧,可以节省很多系统管理的时间. #安装setuptoolyum install setuptool#可以发现执行setup后不全,再安装一个用 ...

  6. MVC 自定义AuthorizeAttribute 实现权限验证

    MVC内置的AuthorizeFilter先于Action/Result过滤器执行,为网站权限验证提供了很好的一套验证机制. 通过自定义的AuthorizeAttribute可以实现对用户权限的验证. ...

  7. asp防注入安全问题

    一.古老的绕验证漏洞虽然古老,依然存在于很多小程序之中,比如一些企业网站的后台,简单谈谈.这个漏洞出现在没有对接受的变量进行过滤,带入数据库判断查询时,造成SQL语句的逻辑问题.例如以下代码存在问题: ...

  8. [java线段树]2015上海邀请赛 D Doom

    题意:n个数 m个询问 每个询问[l, r]的和, 再把[l, r]之间所有的数变为平方(模为9223372034707292160LL) 很明显的线段树 看到这个模(LLONG_MAX为922337 ...

  9. [mock]12月27日

    一开始介绍项目,最后的反馈是,还是说得不清楚,需要再准备准备. 然后两道题,第一题是有个数组,有2*n个数字,从1~n.比如n=3的数组,{1,2,2,3,1,3}.然后两两相同的数字删除,每次删除得 ...

  10. Java知识大全

    http://blog.csdn.net/zhangerqing/article/details/8245560