FrameLayout:帧布局容器为每个加入其中的组件创建一个空白的区域,一个空白区域称为一帧,帧布局容器会把容器中的所有组件一个一个叠放在一起(同一位置),这一点和AWT中的CardLayout相似,但不同点是FrameLayout不能把下面的组件移到上面。相当于是图层效果。
XML属性
|
相关方法
|
说明
|
Android:foreground
|
setForeground(Drawable)
|
设置该帧布局容器的前景图像
|
Android:foregroundGravity
|
setForegroundGravity(int)
|
定义绘制前影图像的gravity属性
|
xml布局文件
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextView android:id="@+id/View01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:width="210px"
- android:height="50px"
- android:background="#ff0000"/>
- <TextView android:id="@+id/View02"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:width="180px"
- android:height="50px"
- android:background="#dd0000"/>
- <TextView android:id="@+id/View03"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:width="150px"
- android:height="50px"
- android:background="#bb0000"/>
- <TextView android:id="@+id/View04"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:width="120px"
- android:height="50px"
- android:background="#990000"/>
- <TextView android:id="@+id/View05"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:width="90px"
- android:height="50px"
- android:background="#770000"/>
- <TextView android:id="@+id/View06"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:width="60px"
- android:height="50px"
- android:background="#550000"/>
- <TextView android:id="@+id/View07"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:width="30px"
- android:height="50px"
- android:background="#330000"/>
- </FrameLayout>
java文件:添加了简单的动画效果
- import java.util.Timer;
- import java.util.TimerTask;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.widget.TextView;
- public class AndroidtestActivity06 extends Activity
- {
- private int currentColor = 0;
- //定义一个颜色数组
- private int[] colors = new int[]
- {
- R.color.color7,
- R.color.color6,
- R.color.color5,
- R.color.color4,
- R.color.color3,
- R.color.color2,
- R.color.color1,
- };
- final int[] names = new int[]
- {
- R.id.View01,
- R.id.View02,
- R.id.View03,
- R.id.View04,
- R.id.View05,
- R.id.View06,
- R.id.View07
- };
- TextView[] views = new TextView[7];
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main06);
- for (int i = 0 ; i < 7 ; i++)
- {
- views[i] = (TextView)findViewById(names[i]);
- }
- final Handler handler = new Handler()
- {
- @Override
- public void handleMessage(Message msg)
- {
- //表明消息来自本程序所发送
- if(msg.what == 0x1122)
- {
- //依次改变7个TextView的背景色
- // for(int i = 0 ; i < 7 - currentColor ; i++)
- // {
- // views[i].setBackgroundResource(colors[i + currentColor]);
- // }
- // for(int i = 7 - currentColor , j = 0 ; i < 7 ; i++ ,j++)
- // {
- // views[i].setBackgroundResource(colors[j]);
- // }
- //循环改变颜色数组
- int a=colors[0];
- for (int i = 0; i < colors.length-1; i++) {
- colors[i]=colors[i+1];
- }
- colors[colors.length-1]=a;
- //循环改变textview颜色
- for (int i = 0; i < views.length; i++) {
- views[i].setBackgroundResource(colors[i]);
- }
- }
- super.handleMessage(msg);
- }
- };
- //定义一个线程周期性的改变currentColor变量值
- new Timer().schedule(new TimerTask()
- {
- @Override
- public void run()
- {
- currentColor++;
- if(currentColor >= 6)
- {
- currentColor = 0;
- }
- //发送一条消息通知系统改变7个TextView组件的背景色
- Message m = new Message();
- //给该消息定义一个标识
- m.what = 0x1122;
- handler.sendMessage(m);
- }
- }, 0 , 100);
- }
- }