Android布局详解之一:FrameLayout

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

 

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

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

看如下的例子:spa

 

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

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

    android:layout_width="fill_parent"blog

    android:layout_height="fill_parent"游戏

    >ip

<TextView utf-8

    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了。虽然用其余的布局也能够,可是用最简单的不是更省系统资源么。

相关文章
相关标签/搜索