美文网首页
Android——FrameLayout

Android——FrameLayout

作者: 四喜汤圆 | 来源:发表于2020-04-10 22:47 被阅读0次

一、作用

布局

二、概念

1.简介

帧布局。帧布局容器为个加入其中的组件创建一个空白的区域(称为一帧),所有每个子组件占据一帧,这些帧会根据gravity属性自动对齐。

这种布局没有丰富的定位方式,所有控件默认摆放在布局的左上角。

2.常用属性

layout_gravity

三、使用

【实现霓虹灯效果】
(1)布局
一个 FrameLayout 中放7个TextView

<?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"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:background="#ff0000" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="180dp"
        android:layout_height="50dp"
        android:background="#dd0000" />

    <TextView
        android:id="@+id/tv3"
        android:layout_width="150dp"
        android:layout_height="50dp"
        android:background="#bb0000" />

    <TextView
        android:id="@+id/tv4"
        android:layout_width="120dp"
        android:layout_height="50dp"
        android:background="#990000" />

    <TextView
        android:id="@+id/tv5"
        android:layout_width="90dp"
        android:layout_height="50dp"
        android:background="#770000" />

    <TextView
        android:id="@+id/tv6"
        android:layout_width="60dp"
        android:layout_height="50dp"
        android:background="#550000" />

    <TextView
        android:id="@+id/tv7"
        android:layout_width="30dp"
        android:layout_height="50dp"
        android:background="#330000" />

</FrameLayout>
显示效果

(2)代码逻辑
准备好一组颜色,colors=[C0,C1,C2,C3,C4,C5,C6],每隔1秒,TextView 换一次颜色。

// 颜色数组
    final int[] colors = new int[] { R.color.color1, R.color.color2, R.color.color3, R.color.color4, R.color.color5,
            R.color.color6, R.color.color7 };

初始化控件

// 控件数组:存放控件id
final int[] names = new int[] {
    R.id.tv1, R.id.tv2, R.id.tv3, R.id.tv4, R.id.tv5, R.id.tv6, R.id.tv7

};

TextView[] mViews = new TextView[7];

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 初始化控件
    for (int i = 0; i < 7; i++) {
        mViews[i] = findViewById(names[i]);
    }
}

开启一个定时器,每隔1000ms换一次颜色。每次换颜色时:确定下来第一个 TextView 要显示的颜色为 C,之后的 TextView 从 colors 中按列表循环顺序读取自己应该显示的颜色

public class FrameLayoutActivity extends AppCompatActivity {
    private static final String TAG = "FrameLayoutActivity";

    // 颜色数组
    final int[] colors = new int[] {
        R.color.color1, R.color.color2, R.color.color3, R.color.color4, R.color.color5,
            R.color.color6, R.color.color7
    };

    // 控件数组:存放控件id
    final int[] names = new int[] {
        R.id.tv1, R.id.tv2, R.id.tv3, R.id.tv4, R.id.tv5, R.id.tv6, R.id.tv7

    };

    TextView[] mViews = new TextView[7];

    // 第一个TextView要显示的颜色,在colors数组中的下标
    int firstColor;
    private MyHandler mMyHandler;

    private class MyHandler extends Handler {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x1122) {
                // 为TextView设置响应颜色
                for (int i = 0; i < mViews.length; i++) {
                    TextView textView = mViews[i];
                    Log.i(TAG, "textView" + i + ",color=" + (i + firstColor) % 7);
                    textView.setBackgroundResource(colors[(i + firstColor) % 7]);
                }
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frame_layout);

        mMyHandler = new MyHandler();
        // 初始化控件
        for (int i = 0; i < 7; i++) {
            mViews[i] = findViewById(names[i]);
        }

        // 开启定时任务,每隔100ms更换一次颜色
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                firstColor++;
                if (firstColor == 7) {
                    firstColor = 0;
                }
                // 发送消息
                Message msg = new Message();
                msg.what = 0x1122;
                mMyHandler.sendMessage(msg);

            }
        }, 0, 1000);
    }
}

四、参考文献

疯狂Android
第一行代码

相关文章

网友评论

      本文标题:Android——FrameLayout

      本文链接:https://www.haomeiwen.com/subject/fkirmhtx.html