Android UI之FrameLayout(帧布局)

Android UI之FrameLayout(帧布局)

说明:帧布局会为每个包含其中的组件开辟一个空白区域(称为帧),这些帧是一层层叠加在一起的,有点类似于一层层覆盖贴上去的海报,后面的组件会把前面的组件覆盖住。

FrameLayout有两个比较特殊的常用属性需要注意:

1 android:foreground

对应方法:setForeground(Drawable) 说明:设置帧布局的前景图像,一般为布局添加pressed状态会用到这个属性来指定一个Drawable类型对象。 举个栗子:

<FrameLayoutandroid:foreground=”@drawable/muogu”android:layout_width=”fill_parent”android:layout_height=”fill_parent”><TextViewandroid:layout_width=”wrap_content”android:layout_height=”wrap_content”android:width=”320dp”android:height=”320dp”android:layout_gravity=”center”android:background=”#3399AA” /><TextViewandroid:layout_width=”wrap_content”android:layout_height=”wrap_content”android:width=”280dp”android:height=”280dp”android:layout_gravity=”center”android:background=”#AA77AA” /><TextViewandroid:layout_width=”wrap_content”android:layout_height=”wrap_content”android:width=”240dp”android:height=”240dp”android:layout_gravity=”center”android:background=”#662288″ /><TextViewandroid:layout_width=”wrap_content”android:layout_height=”wrap_content”android:width=”200dp”android:height=”200dp”android:layout_gravity=”center”android:background=”#895511″ /><ImageViewandroid:layout_width=”160dp”android:layout_height=”160dp”android:layout_gravity=”center”android:scaleType=”fitXY”android:src=”@drawable/nailiu16″ /></FrameLayout>

可以看到设置了foreground为小蘑菇的图片之后,图片直接拉伸占据满了整个FrameLayout,并将FrameLayout中所有的组件都遮盖住了。这就是相对与背景色的前景色的效果。

2 foregroundGravity

对应方法:setForegroundGravity(int) 说明:看名字就知道,肯定是跟前景色摆放有关。没错,这个属性的功能就是定义前景图像的gravity属性,,所以这个属性必须配合foreground使用。而其中的可选项更gravity是一样的,可以在我之前的《 Android UI之LinearLayout(线性布局)》中找到属性表。 举个栗子:

<FrameLayoutandroid:foreground=”@drawable/muogu”android:layout_width=”fill_parent”android:foregroundGravity=”bottom|right”android:layout_height=”fill_parent”><TextViewandroid:layout_width=”wrap_content”android:layout_height=”wrap_content”android:width=”320dp”android:height=”320dp”android:layout_gravity=”center”android:background=”#3399AA” /><TextViewandroid:layout_width=”wrap_content”android:layout_height=”wrap_content”android:width=”280dp”android:height=”280dp”android:layout_gravity=”center”android:background=”#AA77AA” /><TextViewandroid:layout_width=”wrap_content”android:layout_height=”wrap_content”android:width=”240dp”android:height=”240dp”android:layout_gravity=”center”android:background=”#662288″ /><TextViewandroid:layout_width=”wrap_content”android:layout_height=”wrap_content”android:width=”200dp”android:height=”200dp”android:layout_gravity=”center”android:background=”#895511″ /><ImageViewandroid:layout_width=”160dp”android:layout_height=”160dp”android:layout_gravity=”center”android:scaleType=”fitXY”android:src=”@drawable/nailiu16″ /></FrameLayout>

这里的代码更上边的唯一区别就是在FrameLayout中添加了下面这行代码。

android:foregroundGravity=”bottom|right”

可以看到前景图像还原到了原有大小,而且按照设置放到了右下角,这样,FrameLayout中的子组件就显示出来了。

附:引用声明

《疯狂Android讲义(第二版)》 李刚 《2.2.3 帧布局》 电子工业出版社

就得加倍付出汗水,赢得场场精彩

Android UI之FrameLayout(帧布局)

相关文章:

你感兴趣的文章:

标签云: