首先介绍常用布局类

  • 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. webstorm 11 安装配置 grunt 时遇到的问题及解决办法

    想学grunt的可以看看这篇文章,写的很有意思,教程之类的我就不写了,网上很多资料,我就记录下我遇到的问题和解决办法. http://yujiangshui.com/grunt-basic-tutor ...

  2. sublime 配置

    Sublime Text 2是那种让人会一眼就爱上的编辑器,不仅GUI让人眼前一亮,功能更是没的说,拓展性目前来说也完全够用了,网上介绍软件的文章和推荐插件的文章也不少,而且很不错,大家可以去找找自己 ...

  3. 深入浅出 Java 8 Lambda 表达式

    摘要:此篇文章主要介绍 Java8 Lambda 表达式产生的背景和用法,以及 Lambda 表达式与匿名类的不同等.本文系 OneAPM 工程师编译整理. Java 是一流的面向对象语言,除了部分简 ...

  4. c++ ANSI、UNICODE、UTF8互转

        static std::wstring MBytesToWString(const char* lpcszString);    static std::string WStringToMBy ...

  5. 【leetcode】Trapping Rain Water(hard)

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  6. 查看使用的Eclipse版本

    第一种方法 1. 找到Eclipse的解压目录就是你的Eclipse.exe 所在的目录 2. 找到 .eclipseproduct 文件双击打开

  7. SPRING IN ACTION 第4版笔记-第九章Securing web applications-008-使用非关系型数据库时如何验证用户(自定义UserService)

    一. 1.定义接口 Suppose that you need to authenticate against users in a non-relational database suchas Mo ...

  8. ArcGIS Runtime for Android开发教程V2.0(4)基础篇---MapView

    原文地址: ArcGIS Runtime for Android开发教程V2.0(4)基础篇---MapView - ArcGIS_Mobile的专栏 - 博客频道 - CSDN.NET http:/ ...

  9. 【HDOJ】4261 Estimation

    挺不错的一道题,基本思路是dp.关键点是如何求区间内的Sigma|A_i-B_i|.线段树做TLE了,优先队列可以过. /* 4261 */ #include <iostream> #in ...

  10. JsonPath详解

    JsonPath is to JSON what XPATH is to XML, a simple way to extract parts of a given document. JsonPat ...