activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
/>
在res/values目录下,创建一个保存颜色资源的color.xml文件
<?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>
接下来是MainActivity.java
import java.util.Random;
import android.R.integer;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.os.Build;
public class MainActivity extends Activity {
private Handler handler;//创建Handler对象
private static LinearLayout linearLayout;//整体布局
public static TextView[]tv=new TextView[14];//TextView数组
public static String str="小白,天冷多加衣。BY:小黄";//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;//颜色资源数组的下标
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout=(LinearLayout)findViewById(R.id.container);//获取线性布局管理器
int height=this.getResources().getDisplayMetrics().heightPixels;//获取屏幕的高度
for (int i = 0; i < tv.length; i++) {
tv[i]=new TextView(this);//创建一个文本对象
tv[i].setGravity(Gravity.CENTER);//设置文字显示位置
tv[i].setText(String.valueOf(str.charAt(i)));//设置文本框上的文字
tv[i].setWidth(this.getResources().getDisplayMetrics().widthPixels);//设置文本框的宽度
tv[i].setHeight(height/tv.length);//设置文本框的高度
linearLayout.addView(tv[i]);//添加到布局管理器
}
Thread t=new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (!Thread.currentThread().isInterrupted()) {
Message m = handler.obtainMessage();//获取一个Message
m.what=0x101;//设置消息标识
handler.sendMessage(m);//发送消息
try {
Thread.sleep(new Random().nextInt(1000));//休眠一秒
} catch (InterruptedException e) {
// TODO: handle exception
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);
}
};
}
}
在AndroidMainFest.xml文件的<activity>标识中,设置android:theme属性,实现全屏显示
android:theme="@android:style/Theme.Black.NoTitleBar"
运行结果如下: