Android布局详解之一:FrameLayout

原创文章,如有转载,请注明出处:

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

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

看以下的例子:

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

<FrameLayout xmlns: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="第四层"/>

效果如何?没任何效果!

就会犯错误,就会有无数次让自己跌倒的机会出现,

Android布局详解之一:FrameLayout

相关文章:

你感兴趣的文章:

标签云: