FrameLayout的简单运用

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);
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值