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 ...
随机推荐
- Jquery Highcharts 选项配置 说明文档
Highcharts提供大量的选项配置参数,您可以轻松定制符合用户要求的图表,下面为Highcharts常用的最核心的参数选项配置. Chart:图表区选项 Chart图表区选项用于设置图表区相关属性 ...
- unity手游之聊天SDK集成与使用一
手游中都有聊天功能,比如公会,私聊,世界聊天,那么找一个好用,功能强大的SDK的可以节省很多精力,帮助我们提高开发速度与游戏质量. 写本篇博文是为了方便使用这个SDK做聊天模块的程序,避免许多坑,我在 ...
- 2338: [HNOI2011]数矩形 - BZOJ
因为已经看了一眼题解,知道是算中点和长度,相同时构成一个矩形,所以就把所有的线段算出来,然后排序,相同的就更新答案 为了避免误差,我们都用整数存,中点直接相加就行了,没必要除2,长度也只要平方就行了, ...
- uva 10056
概率 Q += p*pow(1-p, i*n+k-1) i = 0,1,2,3...... #include <cstdio> #include <cmath> int mai ...
- Tiny6410 交叉编译helloworld程序
在工作目录下建立helloworld.c文件 #include <stdio.h> main() { printf("helloworld!\n"); } 保存关闭后. ...
- 如何优化 Android Studio 启动、编译和运行速度?
作为一名 Android 程序员,选择一个好的 IDE 工具可以使开发变得非常高效,很多程序员喜欢使用 Google 的 Android Studio来进行开发,但使用起来有时会出现卡顿等问题.本文介 ...
- POJ2739Sum of Consecutive Prime Numbers
http://poj.org/problem?id=2739 题意 :一个正整数能够表示为一个或多个连续素数和,给你一个正整数,让你求,有多少个这样的表示.例如:整数53有两种表示方法,5+7+11+ ...
- SQLite入门与分析(三)---内核概述(2)
写在前面:本节是前一节内容的后续部分,这两节都是从全局的角度SQLite内核各个模块的设计和功能.只有从全局上把握SQLite,才会更容易的理解SQLite的实现.SQLite采用了层次化,模块化的设 ...
- P73、面试题9:斐波那契数列
题目一:写一个函数,输入n,求斐波那契数列(Fibonacci)数列的第n项,斐波那契数列的定义如下: f(n) = {0 n = 0; 1 n = 1; f(n-1)+f(n-2) n& ...
- 2014年最火的 21个JavaScript 框架
下面,我们将会介绍 2014 年最火的 21 款JavaScript 框架,专为前端开发者准备的哦:)众所周知, JavaScript 框架是 JavaScript编程语言最棒的特性之一. JavaS ...