Android FrameLayout布局

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

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

<?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:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ff000000" android:gravity="center" android:text="1"/>
    <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ff654321" android:gravity="center" android:text="2"/>
    <TextView android:layout_width="50dp" android:layout_height="50dp" android:background="#fffedcba" android:gravity="center" android:text="3"/>
</FrameLayout>

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

package cn.sunmeng.FrameLayoutTest;
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 FrameLayoutTestActivity extends Activity {
       private int currentColor = 0;
       //定义一个颜色数组
       final 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.main);
        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]);
                                   }
                            }
                            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);
       }
}
color.xml配置文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="color1">#330000</color>
    <color name="color2">#550000</color>
    <color name="color3">#770000</color>
    <color name="color4">#990000</color>
    <color name="color5">#bb0000</color>
    <color name="color6">#dd0000</color>
    <color name="color7">#ff0000</color>
</resources>
Main.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"
    >
<!-- 依次定义7个TextView,先定义的TextView位于底层
    后定义的TextView位于上层 -->
<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>

转载于:https://my.oschina.net/gesuper/blog/165368

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值