android实例2:FrameLayout布局之霓虹灯
layout xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/view01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:width="320px" android:height="320px" android:background="#f00" /> <TextView android:id="@+id/view02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:width="280px" android:height="280px" android:background="#0f0" /> <TextView android:id="@+id/view03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:width="320px" android:height="320px" android:background="#00f" /> <TextView android:id="@+id/view04" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:width="240px" android:height="240px" android:background="#ff0" /> <TextView android:id="@+id/view05" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:width="200px" android:height="200px" android:background="#f0f" /> <TextView android:id="@+id/view06" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:width="160px" android:height="160px" android:background="#0ff" /> </FrameLayout>
这里我们 还需要额外的一个保存颜色的 colors.xml,它是放在res/values下
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="color1">#f00</color> <color name="color2">#0f0</color> <color name="color3">#00f</color> <color name="color4">#ff0</color> <color name="color5">#f0f</color> <color name="color6">#0ff</color> </resources>
最后是mainActivity.java
package com.example.androidtest1; 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.util.Log; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { private String TAG = "androidtest1"; private int currentcolor = 0; final int[] colors = new int[]{ R.color.color1, R.color.color2, R.color.color3, R.color.color4, R.color.color5, R.color.color6 }; final int[] names = new int[]{ R.id.view01, R.id.view02, R.id.view03, R.id.view04, R.id.view05, R.id.view06 }; TextView[] views = new TextView[names.length]; Handler handler = new Handler() { @Override public void handleMessage(Message msg) { if(msg.what == 0x123) { for(int i=0;i<names.length;i++) { views[i].setBackgroundResource(colors[(i+currentcolor) % names.length]); //i+后 每次颜色往后循环一个 } currentcolor++; } super.handleMessage(msg); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); for(int i=0;i<names.length;i++) { views[i] = (TextView)findViewById(names[i]); } new Timer().schedule(new TimerTask() { @Override public void run() { handler.sendEmptyMessage(0x123); } },0,500); } }