FrameLayout在屏幕上开辟了一块区域,在这块区域中可以添加多个子控件,但是所有的子控件都会被对齐到屏幕的左上角。FrameLayout的大小由其所添加的所有子控件中尺寸最大的那个子控件来控制。如果子控件的大小都一样,同一时刻只能看到最上面的子控件,其他的则被其遮挡(在进行选项卡设计时会用到帧布局)。在FrameLayout中,子控件是通过栈来绘制的,所以后添加的子控件会被绘制在上层。
示例代码如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "short text"/>
<TextView android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor = "#0000FF"
android:textSize="40px"
android:text = "This is a long text ,long text"/>
</FrameLayout>
结果显示如下:
FrameLayout继承ViewGroup类,除了继承父类的属性和方法外,FrameLayout类中还包含了自己特有属性和方法:
android:foreground (setForeground(Drawable))设置绘制在所有子控件之上的内容
android:foregroundGravity(setForegroundGravity(int))设置绘制在所有子控件之上内容的gravity属性