FrameLayout:帧布局容器为每个加入其中的组件创建一个空白的区域,一个空白区域称为一帧,帧布局容器会把容器中的所有组件一个一个叠放在一起(同一位置),这一点和AWT中的CardLayout相似,但不同点是FrameLayout不能把下面的组件移到上面。相当于是图层效果。

XML属性
相关方法
说明
Android:foreground
setForeground(Drawable)
设置该帧布局容器的前景图像
Android:foregroundGravity
setForegroundGravity(int)
定义绘制前影图像的gravity

xml布局文件

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent"> 
  6.       
  7.     <TextView android:id="@+id/View01" 
  8.         android:layout_width="wrap_content" 
  9.         android:layout_height="wrap_content" 
  10.         android:width="210px" 
  11.         android:height="50px" 
  12.         android:background="#ff0000"/> 
  13.     <TextView android:id="@+id/View02" 
  14.         android:layout_width="wrap_content" 
  15.         android:layout_height="wrap_content"   
  16.         android:width="180px" 
  17.         android:height="50px" 
  18.         android:background="#dd0000"/> 
  19.     <TextView android:id="@+id/View03"   
  20.         android:layout_width="wrap_content" 
  21.         android:layout_height="wrap_content" 
  22.         android:width="150px" 
  23.         android:height="50px" 
  24.         android:background="#bb0000"/> 
  25.     <TextView android:id="@+id/View04"   
  26.         android:layout_width="wrap_content" 
  27.         android:layout_height="wrap_content" 
  28.         android:width="120px" 
  29.         android:height="50px" 
  30.         android:background="#990000"/> 
  31.     <TextView android:id="@+id/View05"   
  32.         android:layout_width="wrap_content" 
  33.         android:layout_height="wrap_content" 
  34.         android:width="90px" 
  35.         android:height="50px" 
  36.         android:background="#770000"/> 
  37.     <TextView android:id="@+id/View06"   
  38.         android:layout_width="wrap_content" 
  39.         android:layout_height="wrap_content" 
  40.         android:width="60px" 
  41.         android:height="50px" 
  42.         android:background="#550000"/>      
  43.     <TextView android:id="@+id/View07"   
  44.         android:layout_width="wrap_content" 
  45.         android:layout_height="wrap_content" 
  46.         android:width="30px" 
  47.         android:height="50px" 
  48.         android:background="#330000"/> 
  49.               
  50. </FrameLayout> 

 

Adnroid-FrameLayout布局_职场 

java文件:添加了简单的动画效果

  1. import java.util.Timer;  
  2. import java.util.TimerTask;  
  3.  
  4. import android.app.Activity;  
  5. import android.os.Bundle;  
  6. import android.os.Handler;  
  7. import android.os.Message;  
  8. import android.widget.TextView;  
  9.  
  10. public class AndroidtestActivity06 extends Activity  
  11. {  
  12.     private int currentColor = 0;  
  13.     //定义一个颜色数组  
  14.     private int[] colors = new int[]  
  15.     {  
  16.         R.color.color7,  
  17.         R.color.color6,  
  18.         R.color.color5,  
  19.         R.color.color4,   
  20.         R.color.color3,  
  21.         R.color.color2,  
  22.         R.color.color1,   
  23.     };  
  24.     final int[] names = new int[]  
  25.     {  
  26.         R.id.View01,  
  27.         R.id.View02,  
  28.         R.id.View03,  
  29.         R.id.View04,  
  30.         R.id.View05,  
  31.         R.id.View06,  
  32.         R.id.View07  
  33.     };  
  34.     TextView[] views = new TextView[7];  
  35.     @Override 
  36.     public void onCreate(Bundle savedInstanceState)  
  37.     {  
  38.         super.onCreate(savedInstanceState);  
  39.         setContentView(R.layout.main06);      
  40.         for (int i = 0 ; i < 7 ; i++)  
  41.         {  
  42.             views[i] = (TextView)findViewById(names[i]);  
  43.         }  
  44.         final Handler handler = new Handler()  
  45.         {  
  46.             @Override 
  47.             public void handleMessage(Message msg)  
  48.             {  
  49.                 //表明消息来自本程序所发送  
  50.                 if(msg.what == 0x1122)  
  51.                 {  
  52.                     //依次改变7个TextView的背景色  
  53. //                  for(int i = 0 ; i < 7 - currentColor ; i++)   
  54. //                  {  
  55. //                      views[i].setBackgroundResource(colors[i + currentColor]);  
  56. //                  }  
  57. //                  for(int i = 7 - currentColor , j = 0 ; i < 7 ; i++ ,j++)  
  58. //                  {  
  59. //                      views[i].setBackgroundResource(colors[j]);  
  60. //                  }  
  61.                     //循环改变颜色数组  
  62.                     int a=colors[0];  
  63.                     for (int i = 0; i < colors.length-1; i++) {  
  64.                         colors[i]=colors[i+1];  
  65.                     }  
  66.                     colors[colors.length-1]=a;  
  67.                     //循环改变textview颜色  
  68.                     for (int i = 0; i < views.length; i++) {  
  69.                         views[i].setBackgroundResource(colors[i]);  
  70.                     }  
  71.                 }  
  72.                 super.handleMessage(msg);  
  73.             }  
  74.         };  
  75.         //定义一个线程周期性的改变currentColor变量值  
  76.         new Timer().schedule(new TimerTask()  
  77.         {  
  78.             @Override 
  79.             public void run()  
  80.             {  
  81.                 currentColor++;  
  82.                 if(currentColor >= 6)  
  83.                 {  
  84.                     currentColor = 0;  
  85.                 }  
  86.                 //发送一条消息通知系统改变7个TextView组件的背景色  
  87.                 Message m = new Message();  
  88.                 //给该消息定义一个标识  
  89.                 m.what = 0x1122;  
  90.                 handler.sendMessage(m);   
  91.             }         
  92.         }, 0 , 100);   
  93.     }