1、首先是layout布局页面,使用7个Textview叠加在一起,因此,这里的layout布局使用FrameLayout帧布局,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="@+id/textView3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="@+id/textView4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="@+id/textView5"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="@+id/textView6"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="@+id/textView7"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</FrameLayout>
满屏幕显示效果,因此使当前的布局管理器以及所有的textview均充满了整个屏幕。
2、接下来是关键的一步,java代码如下:
package com.wjl.myandroid1;
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 FrameLayOutTest 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.textView1,
R.id.textView2,
R.id.textView3,
R.id.textView4,
R.id.textView5,
R.id.textView6,
R.id.textView7,
};
TextView[] views = new TextView[7];
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.testview);
for(int i=0;i<7;i++){
views[i] = (TextView) findViewById(names[i]);
}
final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
if(msg.what == 0x1122){//消息来自本程序发送
//依次改变七个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);
}
};
new Timer().schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
currentColor++;
if(currentColor>=6){
currentColor=0;
}
Message m = new Message();
m.what = 0x1122;
handler.sendMessage(m);
}
},0,100);
}
}
解释:
TextView[] views = new TextView[7];
这里是一个views的数组,其中的数据类型均为TextView类型
for(int i=0;i<7;i++){
views[i] = (TextView) findViewById(names[i]);
}
此时便获取了xml里面的七个控件。