关于框架布局是在无特别指定的情况下将所有的控件放在屏幕布局的左上角,并且其中的每一个组件都是一帧(因此也叫作帧布局),后面的组件依次叠放在前边的控件上;FrameLayout布局中存在以下两个常用的关于前景的属性

android:foreground

设置帧布局容器的前景图像

android:foregroundGravity

设置前景图像显示的位置

贴下以下代码,测试帧布局的特点与前景属性

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#efe869"
    android:foreground="@drawable/yueliang"
    android:foregroundGravity="bottom|right">

    <TextView
        android:id="@+id/tv0"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:background="#adecf5"/>

    <TextView
        android:id="@+id/tv1"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:background="#456123"/>

    <TextView
        android:id="@+id/tv2"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#a95656"/>

    <TextView
        android:id="@+id/tv3"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:background="#485a92"/>

</FrameLayout>

 

 

Android FrameLayout控件布局 framelayout布局属性_控件

可以观察到布局的运行效果如图所示:左上角四个TextView依次叠加排布。右下角是一个前景,设置前景的位置为右下。

当然啦,如果全部的组件在FrameLayout中都处于左上角,那么肯定看起来有些别扭,因此我们可以在FrameLayout布局文中内,通过指定其子控件的android:layout_gravity属性,控制子控件在布局文件中所处的位置。

FrameLayout相对于其他的布局,主要的好处就是图层的设计,图层之间可以相互叠加,例如LinearLayout就只能将后边的组件给挤出去了。当然,像之前所介绍的RelativeLayout也可以实现叠加的效果。另外就是FrameLayout比较适用于自定义View与碎片(也相当于是一个Activity)中。