:
1. The first is the code of the main. xml file.
<? Xml version = "1.0" encoding = "UTF-8"?>
<FrameLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
>
<TextView
Android: id = "@ + id/view0"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center"
Android: background = "# ff0000"
Android: height = "200dp"
Android: width = "200dp"/>
<TextView
Android: id = "@ + id/view1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center"
Android: background = "# ff00ff"
Android: height = "180dp"
Android: width = "180dp"/>
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center"
Android: width = "160dp"
Android: height = "160dp"
Android: background = "# bb0000"
Android: id = "@ + id/view2"/>
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: width = "140dp"
Android: height = "140dp"
Android: layout_gravity = "center"
Android: background = "#990000"
Android: id = "@ + id/view3"/>
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: width = "120dp"
Android: height = "120dp"
Android: layout_gravity = "center"
Android: background = "#770000"
Android: id = "@ + id/view4"/>
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: width = "100dp"
Android: height = "100dp"
Android: layout_gravity = "center"
Android: background = "#550000"
Android: id = "@ + id/view5"/>
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: width = "80dp"
Android: height = "80dp"
Android: layout_gravity = "center"
Android: background = "#330000"
Android: id = "@ + id/view6"/>
</FrameLayout>
Emphasize: Content in android: layout_gravity = "center" textview is centered
2. Create a colors. xml file
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<Color name = "color1" >#330000 </color>
<Color name = "color2" >#550000 </color>
<Color name = "color3" >#770000 </color>
<Color name = "color4" >#990000 </color>
<Color name = "color5"> # bb0000 </color>
<Color name = "color6"> # dd0000 </color>
<Color name = "color7"> # ff0000 </color>
</Resources>
3. Source Code LayoutChen. java
Package cn. csdn. activity;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. OS. Message;
Import android. widget. TextView;
Public class LayoutChenActivity extends Activity {
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. view0,
R. id. view1,
R. id. view2,
R. id. view3,
R. id. view4,
R. id. view5,
R. id. view6,
};
TextView view [] = new TextView [7];
Class MyHandler extends Handler {
Int I = 0;
Public void handleMessage (Message msg ){
I ++;
If (I> = 6 ){
I = 1;
}
For (int m = 7-i, n = 0; m <7; m ++, n ++ ){
View [m]. setBackgroundResource (colors [n]);
}
For (int m = 0; m <7-i; m ++ ){
View [m]. setBackgroundResource (colors [m + I]);
}
Sleep (100 );
}
Public void sleep (int j ){
SendMessageDelayed (obtainMessage (0), j );
}
}
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
For (int I = 0; I <7; I ++ ){
View [I] = (TextView) findViewById (names [I]);
}
Final MyHandler myHandler = new MyHandler ();
MyHandler. sleep (100 );
}
The message communication Handler provided by Android is used. This class can implement communication between non-main thread and main thread responsible for UI, and indirectly implement UI update by non-main thread. Because of
SendMessageDelayed (obtainMessage (0), delayMillis); a message is sent by the framework to the handleMessage event. We call the sleep () method again in the handleMessage () method to form a loop call. Before we click on the interface, the two methods will be called cyclically. Foreground images will also be constantly switched to achieve the effect of neon lights.
Author Ren haili