Android:霓虹灯
2016-01-19 15:03
369 查看
activity_main.xml
在res/values目录下,创建一个保存颜色资源的color.xml文件
Main_Activity.java
在AndroidMainFest.xml文件的<activity>标识中,设置android:theme属性,实现全屏显示
这是运行结果
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/ll" android:orientation="vertical" > </LinearLayout>
在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>
Main_Activity.java
package com.example.clolor; import java.util.Random; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.view.Gravity; import android.view.Menu; import android.widget.LinearLayout; import android.widget.TextView; 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.ll);//获取线性布局管理器 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));//休眠1秒钟 }catch(InterruptedException 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); } }; } }
在AndroidMainFest.xml文件的<activity>标识中,设置android:theme属性,实现全屏显示
android:theme="@android:style/Theme.Black.NoTitleBar"
这是运行结果
相关文章推荐
- Android L中水波纹点击效果的实现
- Android 通过JNI实现守护进程,保证Service服务不被杀死
- Android中Notification 提示对话框
- Android控制闪光灯的方法(打开与关闭)
- Android 省市县 三级联动(android-wheel的使用)
- 【android】根据心形方程用代码绘制出图形
- Android中MVP模式与MVC模式比较(含示例)
- android长度单位与内外边距
- Android 判断锁屏、开屏等状态
- Android之AlarmManager闹铃提醒工具类
- Android Volley 获取磁盘已有缓存数据
- 获取Android系统所有内置SD卡以及外置SD卡路径
- Android 绘图基础Canvas
- 详解Android中AsyncTask的使用
- Android气泡效果实现方法
- android中提示&对话框----Notification
- Android WebRTC开发环境设置
- Android LayoutInflater详解
- android 对未签名的apk签名
- Android事件分发