FrameLayout布局

FrameLayout布局号称Android五大布局最简单的一种。可能正因为它简单,以前做开发的时候都没有注意到它。这次开发需要一个布局控件能够包裹我的TextView,就把它拿出来了。现在就看看他的用法。

在FrameLayout布局中,整个界面被当成一块空白备用区域,所有的子元素都不能被指定放置的位置,它们统统放于这块区域的左上角,并且后面的子元素直接覆盖在前面的子元素之上,将前面的子元素部分和全部遮挡。显示效果如下,第一个TextView被第二个TextView完全遮挡,第三个TextView遮挡了第二个TextView的部分位置。

1 <?xml version="1.0" encoding="utf-8"?>
2 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
3     <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ff000000" android:gravity="center" android:text="1"/>
4     <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ff654321" android:gravity="center" android:text="2"/>
5     <TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#fffedcba" android:gravity="center" android:text="3"/>
6 </FrameLayout>

FrameLayout的用法就是这么简单。有人用frame的这个特性,做出了霓虹灯的效果。这里拿出来给列位看官瞅瞅。先上个图看看效果。图中7个颜色在程序的控制下会依次循环往复的移动,仿佛灯在闪烁。

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

01 <?xml version="1.0" encoding="utf-8"?>
02 <resources>
03     <color name="color1">#330000</color>
04     <color name="color2">#550000</color>
05     <color name="color3">#770000</color>
06     <color name="color4">#990000</color>
07     <color name="color5">#bb0000</color>
08     <color name="color6">#dd0000</color>
09     <color name="color7">#ff0000</color>
10 </resources>
Main.xml配置文件

01 <?xml version="1.0" encoding="utf-8"?>
02 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
03     android:orientation="vertical"
04     android:layout_width="fill_parent"
05     android:layout_height="fill_parent"
06     >
07 <!-- 依次定义7个TextView,先定义的TextView位于底层
08     后定义的TextView位于上层 -->
09 <TextView android:id="@+id/View01"
10     android:layout_width="wrap_content"
11     android:layout_height="wrap_content"
12     android:width="210px"
13     android:height="50px"
14     android:background="#ff0000"
15     />
16 <TextView android:id="@+id/View02"
17     android:layout_width="wrap_content"
18     android:layout_height="wrap_content"
19     android:width="180px"
20     android:height="50px"
21     android:background="#dd0000"  
22     />
23 <TextView android:id="@+id/View03"
24     android:layout_width="wrap_content"
25     android:layout_height="wrap_content"
26     android:width="150px"
27     android:height="50px"
28     android:background="#bb0000"  
29     />
30 <TextView android:id="@+id/View04"
31     android:layout_width="wrap_content"
32     android:layout_height="wrap_content"
33     android:width="120px"
34     android:height="50px"
35     android:background="#990000"  
36     />
37 <TextView android:id="@+id/View05"
38     android:layout_width="wrap_content"
39     android:layout_height="wrap_content"
40     android:width="90px"
41     android:height="50px"
42     android:background="#770000"  
43     />
44 <TextView android:id="@+id/View06"
45     android:layout_width="wrap_content"
46     android:layout_height="wrap_content"
47     android:width="60px"
48     android:height="50px"
49     android:background="#550000"  
50     />
51 <TextView android:id="@+id/View07"
52     android:layout_width="wrap_content"
53     android:layout_height="wrap_content"
54     android:width="30px"
55     android:height="50px"
56     android:background="#330000"  
57     />    
58 </FrameLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值