FrameLayout单帧布局,在单帧布局中整个界面被当成一块空白备用区域,所有的子元素都不能被指定放置的位置,它们统统放于这块区域的左上角,并且后面的子元素直接覆盖在前面的子元素之上,将前面的子元素部分和全部遮挡。
FrameLayout的大小由其所添加的所有子控件中尺寸最大的那个子控件来控制。如果子控件的大小都一样,同一时刻只能看到最上面的子控件,其他的则被其遮挡(在进行选项卡设计时会用到帧布局)。在FrameLayout中,子控件是通过栈来绘制的,所以后添加的子控件会被绘制在上层。
FrameLayout继承ViewGroup类,除了继承父类的属性和方法外,FrameLayout类中还包含了自己特有属性和方法:
android:foreground (setForeground(Drawable))设置绘制在所有子控件之上的内容。
android:foregroundGravity(setForegroundGravity(int))设置绘制在所有子控件之上内容的gravity属性
。
显示效果:
XML:
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textColor="#00aa00"
android:textSize="15dp" />
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textColor="#aaaa00"
android:textSize="30dp" />
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textColor="#aaaaaa"
android:textSize="45dp" />
注意:在FrameLayout布局里面android:layout_margin的各种属性必须依赖于android:layout_gravity,也就是说,要想margin生效,必须设定view的layout_gravity属性。