FrameLayout是最简单的一种布局,放在布局里的所有控件,都将按照层次堆叠在屏幕的左上角,后加进来的控件覆盖前面的控件,利用这一特性,我们可以做出一些漂亮的特效(如图)。
Layout界面的设置:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/v1"
android:layout_width="320dp"
android:layout_height="320dp"
android:background="#ff0000"
android:layout_gravity="center"/>
<Button
android:id="@+id/v2"
android:layout_width="240dp"
android:layout_height="240dp"
android:background="#00ff00"
android:layout_gravity="center"/>
<Button
android:id="@+id/v3"
android:layout_width="160dp"
android:layout_height="160dp"
android:background="#0000ff"
android:layout_gravity="center"/>
<Button
android:id="@+id/v4"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#ff1243"
android:layout_gravity="center"/>
<Button
android:id="@+id/v5"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#324678"
android:layout_gravity="center"/>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_gravity="center"/>
</FrameLayout>
MainActivity 方法:
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button[] bts = new Button[5];
bts[0] = (Button) findViewById(R.id.v1);
bts[1] = (Button) findViewById(R.id.v2);
bts[2] = (Button) findViewById(R.id.v3);
bts[3] = (Button) findViewById(R.id.v4);
bts[4] = (Button) findViewById(R.id.v5);
final List<Integer> colors = new ArrayList<Integer>();
colors.add(0xffff0000);
colors.add(0xff00ff00);
colors.add(0xffff00ff);
colors.add(0x00ffff00);
colors.add(0xffff00);
colors.add(0xffffff00); //必须使用十六进制,而且必须是八位
bts[0].postDelayed(new Runnable() {
//子线程不能修改控件
//延迟400s后,在主线程中执行Runnable中的run方法,这样不会阻塞主线程
@Override
public void run() {
for (int i = 0; i < 5; i++) {
bts[i].setBackgroundColor(colors.get(i));
}
//在这里设置成1的话,是往里回旋,-1则往外。
Collections.rotate(colors, -1);
bts[0].postDelayed(this, 400);
}
}, 400);
}
}