1、布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ll" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="color1">#ffff0000</color> <color name="color2">#ffff6600</color> <color name="color3">#ffffff00</color> <color name="color4">#ff00ff00</color> <color name="color5">#ff00ffff</color> <color name="color6">#ff0000ff</color> <color name="color7">#ff6600ff</color> </resources>
private Handler handler;//创建Handler对象 private static LinearLayout linearLayout;//整体布局 public static TextView[] tv = new TextView[14];//TextView数组 int[] bgColor=new int[]{R.color.color1,R.color.color2,R.color.color3, R.color.color4,R.color.color5,R.color.color6,R.color.color7}; //使用颜色资源 private int index=0; //当前颜色值
linearLayout = (LinearLayout)findViewById(R.id.ll);//获取线性布局管理器 int height = this.getResources().getDisplayMetrics().heightPixels;//获取屏幕的高度 for (int i = 0; i < tv.length; i++) { tv[i] = new TextView(this);//创建一个文本框对象 tv[i].setWidth(this.getResources().getDisplayMetrics().widthPixels);//设置文本框的宽度 tv[i].setHeight(height/tv.length);//设置文本框的高度 linearLayout.addView(tv[i]);//将TextView组件添加到线性布局管理器中 }
Thread t = new Thread(new Runnable() { @Override public void run() { while(!Thread.currentThread().isInterrupted()){ Message m = handler.obtainMessage();//获取一个Message m.what = 0x101;//设置消息标识 handler.sendMessage(m);//发哦送消息 try { Thread.sleep(new Random().nextInt(1000));//休眠1秒钟 } catch (Exception e) { e.printStackTrace();//输出一场信息 } } } }); t.start();
handler = new Handler(){ public void handleMessage(Message msg) { int temp = 0;//临时变量 if(msg.what == 0x101){ for (int i = 0; i < tv.length; i++) { temp = new Random().nextInt(bgColor.length);//昌盛一个随机数 //去掉重复的并且相邻的颜色 if(index==temp){ temp++; if(temp==bgColor.length){ temp=0; } } index = temp; //为文本框设置背景 tv[i].setBackgroundColor(getResources().getColor(bgColor[index])); } } super.handleMessage(msg); }; };
android:theme="@android:style/Theme.Black.NoTitleBar">