原创文章,如有转载,请注明出处:http://blog.csdn.net/yihui823/article/details/6702273

FrameLayout是最简单的布局了。所有放在布局里的控件,都按照层次堆叠在屏幕的左上角。后加进来的控件覆盖前面的控件。

在FrameLayout布局里,定义任何空间的位置相关的属性都毫无意义。控件自动的堆放在左上角,根本不听你的控制。

看以下的例子:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textSize="50dip"

android:textColor="#ffffff"

android:text="第一层"/>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textSize="40dip"

android:textColor="#ffff00"

android:text="第二层"/>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textSize="30dip"

android:textColor="#ff00ff"

android:text="第三层"/>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textSize="20dip"

android:textColor="#00ffff"

android:text="第四层"/>

</FrameLayout>

效果如下图:layoutpic001

变化1

我们现在来尝试改变一下他们的位置。把第一个和第二个文本框改成:

<TextView

android:id="@+id/tv1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textSize="50dip"

android:textColor="#ffffff"

android:text="第一层"/>

<TextView

android:id="@+id/tv2"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textSize="40dip"

android:textColor="#ffff00"

android:layout_toRightOf="@id/tv1"

android:text="第二层"/>

也就是说,让第二个文本框放在第一个文本框的右边。我们来看看效果。看到了没?还是一样的不变吧。

变化2

我们来尝试下android:gravity属性。把第三个文本框改成:

<TextView

android:id="@+id/tv3"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textSize="30dip"

android:textColor="#ff00ff"

android:gravity="right"

android:text="第三层"/>

看看效果如何?天哪!竟然没有覆盖,而是错开了!!!

layoutpic002

首先呢,我们不要大惊小怪。这个现象并不说明FrameLayout失效了。gravity属性,是控制控件内部文本的格式的。而我们看我们控件的宽的属性是什么?是“fill_parent”,也就是说,我们文本框的宽度就是屏幕的宽度。那么android:gravity="right"文本靠右,而文本框本身还是左上堆叠在一起的。不信,我们再来改改:

<TextView

android:id="@+id/tv3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textSize="30dip"

android:textColor="#ff00ff"

android:gravity="right"

android:text="第三层"/>

我们让第三个文本框的宽度自适应,也就是保证显示全文字即可。这个时候看一下效果呢?是不是打回原形啦?哈哈哈。

变化3

我们再来试试” android:layout_centerVertical”属性。把第四个文本框改成:

<TextView

android:id="@+id/tv4"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textSize="20dip"

android:textColor="#00ffff"

android:layout_centerVertical="true"

android:text="第四层"/>

效果如何?没任何效果!

总结一下,经过以上的3个实验,我们知道FrameLayout根本无法控制他的子控件的位置。所有的控件都是左上对其。但是控件本身是可以控制自 己内部的布局的。所以利用透明,也是可以完成一些简单的功能的。例如屏幕四个角各显示一些文字(是显示文字,没法放置控件)。因为每一层覆盖下一层的时 候,如果用透明背景,则下一层不会被背景覆盖。

什么是透明背景?这个……说来话长啦。偷个懒,下次写一下透明的处理。

是不是有人会问,这么简单的Layout有什么用?我想还是有它存在的价值的。

当你需要自己写一个View的时候,在View里面已经完成了你的逻辑(例如游戏^_^),那么这个View只需要一个容器放置,就可以使用FrameLayout了。虽然用其他的布局也可以,但是用最简单的不是更省系统资源么。

 

Android布局详解之一:FrameLayout的更多相关文章

  1. Android 布局详解

    Android 布局详解 1.重用布局 当一个布局文件被多处使用时,最好<include>标签来重用布局. 例如:workspace_screen.xml的布局文件,在另一个布局文件中被重 ...

  2. Android(java)学习笔记93:Android布局详解之一:FrameLayout

    FrameLayout是最简单的布局了.所有放在布局里的控件,都按照层次堆叠在屏幕的左上角.后加进来的控件覆盖前面的控件. 在FrameLayout布局里,定义任何空间的位置相关的属性都毫无意义.控件 ...

  3. Android(java)学习笔记32:Android布局详解之一:FrameLayout

    1. FrameLayout是最简单的布局了.所有放在布局里的控件,都按照层次堆叠在屏幕的左上角.后加进来的控件覆盖前面的控件. 在FrameLayout布局里,定义任何空间的位置相关的属性都毫无意义 ...

  4. android布局详解

      http://blog.163.com/zhangzheming_282/blog/static/117920962013072502787/   AbsoluteLayout——绝对布局   必 ...

  5. Android 布局详解 -三表格布局(TableLayout)以及重要属性

              TableLayout跟TableRow 是一组搭配应用的布局,TableLayout置底,TableRow在TableLayout的上方,而Button.TextView等控件就 ...

  6. Android开发重点难点1:RelativeLayout(相对布局)详解

    前言 啦啦啦~博主又推出了一个新的系列啦~ 之前的Android开发系列主要以完成实验的过程为主,经常会综合许多知识来写,所以难免会有知识点的交杂,给人一种混乱的感觉. 所以博主推出“重点难点”系列, ...

  7. Android 布局学习之——Layout(布局)详解二(常见布局和布局参数)

    [Android布局学习系列]   1.Android 布局学习之——Layout(布局)详解一   2.Android 布局学习之——Layout(布局)详解二(常见布局和布局参数)   3.And ...

  8. Android Notification 详解(一)——基本操作

    Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...

  9. Android Notification 详解——基本操作

    Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...

随机推荐

  1. Surrounded Regions

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

  2. python学习小结7:变量类型

    变量存储在内存中的值.这就意味着在创建变量时会在内存中开辟一个空间. 基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中. 因此,变量可以指定不同的数据类型,这些变量可以存储整 ...

  3. win8安装matlab7.0

    win8和win7下安装matlab7.0要注意许多地方,其实安装最新版一般都是没有问题的. 不过最新版太大,校园网下载太难,所以还是用7.0 基本上在百度经验上已经包括了大部分的注意事项了,可以参考 ...

  4. 推荐acm题目

    杭电  http://acm.hdu.edu.cn/onlineuser.php. 浙大  http://acm.zju.edu.cn/onlinejudge/submit.do?problemId= ...

  5. 机器学习中的数学-矩阵奇异值分解(SVD)及其应用

    转自:http://www.cnblogs.com/LeftNotEasy/archive/2011/01/19/svd-and-applications.html 版权声明: 本文由LeftNotE ...

  6. Hadoop集群中pig工具的安装过程记录

    在Hadoop环境中安装了pig工具,安装过程中碰到了一些问题,在此做一下记录:   主要安装流程参考:http://www.cnblogs.com/yanghuahui/p/3768270.html ...

  7. PHP读取xml方法讲解

    一,什么是xml,xml有什么用途 XML(Extensible Markup Language)即可扩展标记语言,它与HTML一样,都是SGML(Standard Generalized Marku ...

  8. asp.net mvc4 使用KindEditor文本编辑器

    最近做项目要用文本编辑器,编辑器好多种,这里介绍KindEditor在asp.net mvc4中的使用方法. 一.准备工作: 1.下载KindEditor.去官网:http://www.kindsof ...

  9. js刷新

    jquery刷新页面的实现代码(局部及全页面刷新) 发布:mdxy-dxy 字体:[增加 减小] 类型:转载 jquery刷新页面的实现代码(局部及全页面刷新) ,需要的朋友可以参考下. 局部刷新: ...

  10. 【转载】SSH框架总结(将网上朋友写的给整合了下)

    一.Struts 在没有学习SSH框架前,我们一般采用Jsp+javabean+servlet开发,这里就是MVC架构.而Struts其实就是替代了Servlet,我们知道Servlet在一般的开发中 ...