帧布局(FrameLayout)在屏幕上开辟了一块区域,在这块区域中可以添加多个子控件,但是所有的子控件都会被对齐到屏幕的左上角。帧布局的大小由其下属子控件中尺寸最大的那个子控件来控制。如果子控件的大小都一样,同一时刻只能看到最上面的子控件,其他的则被其遮挡(在进行选项卡设计时会用到帧布局)。
FrameLayout继承自ViewGroup类,除了继承自父类的属性和方法,FrameLayout类中也包含了自己特有的一些属性和方法,见下表:
属性名称 | 对应方法 | 描述 |
android:foreground | setForeground(Drawable) | 设置绘制在所有子控件之上的内容 |
android:foregroundGravity | setForegroundGravity(int) | 设置绘制在所有子控件之上内容的gravity属性 |
提示:在FrameLayout中,子控件是通过栈来绘制的,所以后添加的子控件会被绘制在上层。
下面就来看一个帧布局的例子效果图:
其中Main.xml代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout
- android:id="@+id/FrameLayout01"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#FFFFFF"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <TextView
- android:text="大字体"
- android:id="@+id/TextView01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="60px"
- android:textColor="#00FF00"
- >
- </TextView>
- <TextView
- android:text="中字体"
- android:id="@+id/TextView02"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="40px"
- android:textColor="#FF0000"
- >
- </TextView>
- <TextView
- android:text="小字体"
- android:id="@+id/TextView03"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="20px"
- android:textColor="#0000FF"
- >
- </TextView>
- </FrameLayout>
Activity代码为:
- package com.sunchis;
- import android.app.Activity;
- import android.os.Bundle;
- public class Android extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main); //设置屏幕
- }
- }