Android UI -- 布局介绍(布局包括FrameLayout, LinearLayout, RelativeLayout, GridLayout)
首先介绍常用布局类
- FrameLayout 最简单的布局管理器。
这个布局管理类有几个特性:
- 添加组件默认在左上角的。
- 如果添加多个组件会叠加到一起,并且都在左上角。(可以通过一gravity属性改变叠加情况)
- 后添加的组件在上层。
- 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)的更多相关文章
- 【转】Android UI 五种布局
在一个Android应用中,Layout是开发中的一个很重要环节,Layout是组成UI不可缺少的一部分. ## Android UI 核心类 在Android应用构建UI的方法有以下几种: 单纯使用 ...
- Android UI组件:布局管理器
为了更好的管理Android应用的用户界面中的组件,Android提供了布局管理器.通过使用布局管理器,Android应用的图形用户界面具有良好的平台无关性.通常,推荐使用布局管理器来管理组件的分布. ...
- Android UI测量、布局、绘制过程探究
在上一篇博客<Android中Activity启动过程探究>中,已经从ActivityThread.main()开始,一路摸索到ViewRootImpl.performTraversals ...
- Android UI 使用HTML布局(直接打开server网页)
非常多时候我们用HTML布局会更方便直接,记录一下. 我如今主要是直接调用server的网页(实际上是jsp的,仅仅是返回的是html).所以须要联网,第一步加入权限. <uses-permis ...
- android UI进阶之用【转】
android UI进阶之用ViewPager实现欢迎引导页面 摘要: ViewPager需要android-support-v4.jar这个包的支持,来自google提供的一个附加包.大家搜下即可. ...
- Android开发1:基本UI界面设计——布局和组件
前言 啦啦啦~本学期要开始学习Android开发啦~ 博主在开始学习前是完完全全的小白,只有在平时完成老师要求的实验的过程中一步一步学习~从此篇博文起,博主将开始发布Android开发有关的博文,希望 ...
- 【详细】Android入门到放弃篇-YES OR NO-》各种UI组件,布局管理器,单元Activity
问:达叔,你放弃了吗? 答:不,放弃是不可能的,丢了Android,你会心疼吗?如果别人把你丢掉,你是痛苦呢?还是痛苦呢?~ 引导语 有人说,爱上一个人是痛苦的,有人说,喜欢一个人是幸福的. 人与人之 ...
- Android五大布局介绍&属性设置大全
前言 在进行Android开发中,常常需要用到各种布局来进行UI的绘制,今天我们就来讲下Android开发中最常用的五大布局介绍和相关属性的设置. 目录 Android五大布局介绍&属性设置. ...
- 让我们创建屏幕- Android UI布局和控件
下载LifeCycleTest.zip - 278.9 KB 下载ViewAndLayoutLessons_-_Base.zip - 1.2 MB 下载ViewAndLayoutLessons_-_C ...
随机推荐
- Xcode界面简介
创建 Object-C 程序 进入主界面方式 : 打开 Xcode 之后, 可以使用 Xcode 菜单, 但是必须创建一个 Object-C 工程 或者 导入 一个 Object-C 工程才可以进入主 ...
- 域名的a记录转过来他的公网ip
首先 客户要我把域名 和项目进行绑定客户需要提供万网或者新网的账户,登进去域名管理 选项域名管理的A记录定向到那个公网ip上 与服务器做绑定然后 在服务器的iis 上 加个主机头 输入主机头名称 也 ...
- springMVC+MyBatis+Spring 整合(4) ---解决Spring MVC 对AOP不起作用的问题
解决Spring MVC 对AOP不起作用的问题 分类: SpringMVC3x+Spring3x+MyBatis3x myibaits spring J2EE2013-11-21 11:22 640 ...
- CQRS学习——最小单元的Cqrs(CommandEvent)[其一]
[说明:博主采用边写边思考的方式完成这一系列的博客,所以代码以附件为准,文中代码仅为了说明.] 结构 在学习和实现CQRS的过程中,首要参考的项目是这个[http://www.cnblogs.com/ ...
- 团体程序设计天梯赛-练习集L2-010. 排座位
L2-010. 排座位 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 布置宴席最微妙的事情,就是给前来参宴的各位宾客安排座位. ...
- 如何设置Samza的metrics
参考这个里边对API的调用 http://samza.incubator.apache.org/learn/documentation/0.7.0/container/metrics.html 参考这 ...
- poj The Clocks 高斯消元
由于数据量不大,所以这题有很多解法. 我用的是高斯消元化为逆矩阵解决的…… 代码如下: #include<stdio.h> #include<iostream> using n ...
- 李洪强漫谈iOS开发[C语言-013]-常量
// // main.m // 09 - 常量 // // Created by 李洪强 on 16/7/17. // Copyright © 2016年 李洪强. All rights re ...
- 让阿里云的Centos,PHP组件 ImageMagick支持png和jpeg格式
我们在Centos安装ImageMagick教程中讲述了如何安装ImageMagick,安装完毕之后发现程序并不支持png和jpeg格式的图片,但是这两种图片又是我们平时所常见的,所以我们还要进一步地 ...
- [mock]12月11日
给出一个二叉搜索树的先序遍历,要求重新构造出这个二叉搜索树.一开始给出了一个递归的算法,指出复杂度有更优的解法.然后想出来一个O(n)的算法,就是使用一个栈,然后依次判断新的节点比原来的大还是小,然后 ...