帧布局(FrameLayout)之跑马灯

各位看客,今天主要学习的是,布局之帧布局FrameLayout。

这玩意从何而来:FrameLayout  直接继承至 ViewGroup组件。

它的子元素该受啥控制:它的子元素受FrameLayout.LayoutParams 控制。没错,受它控制呢,咱们就可以设置 android.layout_gravity 属性 (相对于父容器的对齐方式设置)

它的特点:它为每个加入的组件都创建一个空白区域(一帧),将组件一个个的叠加在一起,叠加顺序是 最后的组件显示在最上层。

基本概念就是这么个情况!直接完成跑马灯示例

新建项目不多说。

1.准备颜色资源

在 res/values 文件夹下 新建  一个 color.xml  颜色资源文件

<?xml version="1.0" encoding="utf-8" ?>

<resources>

    <color name="l_fl_c_c1">#27AF7D</color>

    <color name="l_fl_c_c2">#AF2777</color>

    <color name="l_fl_c_c3">#B1981A</color>

    <color name="l_fl_c_c4">#9B15D4</color>

    <color name="l_fl_c_c5">#13A8AA</color>

    <color name="l_fl_c_c6">#B89009</color>

</resources>
View Code

 


2.新建布局文件 activity_frame_layout.xml

<?xml version="1.0" encoding="utf-8" ?>

<FrameLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="#000000">

     <TextView 

        android:id="@+id/l_fl_txtv0"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_gravity="top|center" 

        android:textColor="#fff"

        android:text="轮换颜色值测试"/>

    

    <TextView 

        android:id="@+id/l_fl_txtv1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:width="320dp"

        android:height="320dp"

        android:layout_gravity="center" 

        android:background="@color/l_fl_c_c1"/>

    

    <TextView 

        android:id="@+id/l_fl_txtv2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:width="280dp"

        android:height="280dp"

        android:layout_gravity="center" 

        android:background="@color/l_fl_c_c2"/>

    <TextView 

        android:id="@+id/l_fl_txtv3"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:width="240dp"

        android:height="240dp"

        android:layout_gravity="center" 

        android:background="@color/l_fl_c_c3"/>

    <TextView 

        android:id="@+id/l_fl_txtv4"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:width="200dp"

        android:height="200dp"

        android:layout_gravity="center" 

        android:background="@color/l_fl_c_c4"/>

    <TextView 

        android:id="@+id/l_fl_txtv5"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:width="160dp"

        android:height="160dp"

        android:layout_gravity="center" 

        android:background="@color/l_fl_c_c5"/>

    <TextView 

        android:id="@+id/l_fl_txtv6"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:width="120dp"

        android:height="120dp"

        android:layout_gravity="center" 

        android:background="@color/l_fl_c_c6"/>

   

</FrameLayout>
View Code

 

 

3. 后台代码实现

import java.util.Timer;

import java.util.TimerTask;



import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.app.Activity;

import android.view.Menu;

import android.widget.TextView;



public class FrameLayoutActivity extends Activity {

    

    private int currentColor=0;

    

    //颜色资源

    final int[]  colors=new int[]{

            R.color.l_fl_c_c1,

            R.color.l_fl_c_c2,

            R.color.l_fl_c_c3,

            R.color.l_fl_c_c4,

            R.color.l_fl_c_c5,

            R.color.l_fl_c_c6};

    

    //控件ID数组

    final int[] txtvIds=new int[]{ 

            R.id.l_fl_txtv1,

            R.id.l_fl_txtv2,

            R.id.l_fl_txtv3,

            R.id.l_fl_txtv4,

            R.id.l_fl_txtv5,

            R.id.l_fl_txtv6}; 

    

    

    TextView[] txtvs=new TextView[txtvIds.length];

    

    Handler handler=new Handler(){

        @Override

        public void handleMessage(Message msg) {

            // TODO Auto-generated method stub

            String mes="轮换颜色值测试(组件ID--颜色值) // ";

            if(msg.what==200){

                for(int i=0;i<txtvIds.length;i++){

                    txtvs[i].setBackgroundResource(colors[(i+currentColor) % txtvIds.length]);

                    mes+=txtvs[i].getId()+"--"+colors[(i+currentColor) % txtvIds.length]+"//";

                }

                TextView t1= (TextView)findViewById(R.id.l_fl_txtv0);

                t1.setText(mes);

                currentColor++;

            }

            super.handleMessage(msg);

        }

    };

    

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_frame_layout);

        for(int i=0;i<txtvIds.length;i++){

            txtvs[i]=(TextView)findViewById(txtvIds[i]);

        }

        //定义一个线程周期 改变currentColor值

        new Timer().schedule(new TimerTask() {

            @Override

            public void run() {

                handler.sendEmptyMessage(200);

            }

        },0,250);

    }



    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.frame_layout, menu);

        return true;

    }



}
View Code

注: 这里为什么不直接在run()  方法中实现轮换颜色的代码,因为安卓中 View 和 Ui组件  不是线程安全的,规定不允许启动线程来访问用户界面的UI组件。

 

以上就是跑马灯示例的实现,大伙可以 去调整 几个 textview  组件 的  android.layout_gravity 或 layout_margin 等影响布局的属性,看会发生什么变化,又会有哪些不变化。

我自己调试出的结果是:不管布局位置怎么变化,组件显示的方式都是 后面的组件显示在最上层。

 

你可能感兴趣的:(FrameLayout)