帧式布局.

(一)帧式布局概述

1、布局特点

帧式布局是一种层叠式的布局,后添加的控件会层叠在先添加的控件上。

2、继承关系图

FrameLayout类是ViewGroup的子类
在这里插入图片描述

3、常用属性

属性含义
scrollbars滚动条(none、horizontal、vertical)
layout_marginTop上边距
layout_marginBottom下边距
layout_marginLeft左边距
layout_marginRight右边距
paddingLeft左内边距
paddingRight右内边距
paddingTop上内边距
paddingBottom下内边距
background背景(背景色、背景图、背景选择器)

(二)案例演示:切换颜色

1、创建安卓应用

基于 Empty Activity 创建安卓应用 - SwitchColor
在这里插入图片描述单击【Finish】按钮
在这里插入图片描述

2、主布局资源文件

主布局资源文件 - activity_main.xml
在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp">
        <TextView
            android:id="@+id/tv_bottom"
            android:layout_width="300dp"
            android:layout_height="300dp"
            android:layout_gravity="center"
            android:background="#ff0000"
            android:text="@string/bottom"
            android:textSize="30sp"/>

        <TextView
            android:id="@+id/tv_middle"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_gravity="center"
            android:background="#00ff00"
            android:text="@string/middle"
            android:textSize="30sp"/>

        <TextView
            android:id="@+id/tv_top"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="center"
            android:background="#0000ff"
            android:text="@string/top"
            android:textSize="30sp"/>




    </FrameLayout>

    <Button
        android:id="@+id/btn_switch_color"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:onClick="doSwitchColor"
        android:textSize="20sp"
        android:text="@string/switch_color" />



</LinearLayout>

3、字符串资源文件

字符串资源文件 - strings.xml
在这里插入图片描述

<resources>
    <string name="app_name">帧式布局:切换颜色</string>
    <string name="switch_color">切换颜色</string>
    <string name="bottom">底层</string>
    <string name="middle">中层</string>
    <string name="top">顶层</string>
</resources>

此时,查看界面预览效果
在这里插入图片描述

4、主界面实现功能

主界面类 - MainActivity
在这里插入图片描述

定义变量
在这里插入图片描述
通过资源标识符获取控件实例
在这里插入图片描述
编写切换颜色单击事件处理方法

在这里插入图片描述

package net.xh.switch_color;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView tvTOP;
    private TextView tvMiddle;
    private TextView tvBottom;
    private int clickCount;//按钮单机次数
    private int[] colors;//颜色数组

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        //获取控件
        tvTOP = findViewById(R.id.tv_top);
        tvMiddle = findViewById(R.id.tv_middle);
        tvBottom = findViewById(R.id.tv_bottom);
    }

    public void doSwitchColor(View view){
        //累加按钮单机次数
        clickCount++;
        //只有三种颜色切换,点击次数对3求余
        clickCount = clickCount % 3;
        //根据单击次数确定颜色方案
        switch (clickCount){
            case 0://红,绿,蓝
                colors = new int[]{Color.RED,Color.GREEN,Color.BLUE};
                break;
            case 1://绿,蓝,红
                colors = new int[]{Color.GREEN,Color.BLUE,Color.RED};
                break;
            case 2://蓝,红,绿
                colors = new int[]{Color.BLUE,Color.RED,Color.GREEN};
                break;

        }
        //根据颜色方案来设置三层标签背景
        tvBottom.setBackgroundColor(colors[0]);
        tvMiddle.setBackgroundColor(colors[1]);
        tvTOP.setBackgroundColor(colors[2]);

    }
}
package net.xh.switch_color;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView tvTOP;
    private TextView tvMiddle;
    private TextView tvBottom;
    private int[] colors;//颜色数组

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取控件
        tvTOP = findViewById(R.id.tv_top);
        tvMiddle = findViewById(R.id.tv_middle);
        tvBottom = findViewById(R.id.tv_bottom);
        //初始化颜色数组
        colors = new int[]{Color.RED,Color.GREEN,Color.BLUE};
    }

    public void doSwitchColor(View view){
        //通过颜色数组切换颜色
        int temp = colors[0];
        colors[0] = colors[1];
        colors[1] = colors[2];
        colors[2] = temp;
        //根据颜色方案来设置三层标签背景
        tvBottom.setBackgroundColor(colors[0]);
        tvMiddle.setBackgroundColor(colors[1]);
        tvTOP.setBackgroundColor(colors[2]);

    }
}

5、启动应用,查看效果

单击【切换颜色】按钮
在这里插入图片描述

package net.xh.switch_color;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView tvTOP;
    private TextView tvMiddle;
    private TextView tvBottom;
    private int[] colors;//颜色数组

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取控件
        tvTOP = findViewById(R.id.tv_top);
        tvMiddle = findViewById(R.id.tv_middle);
        tvBottom = findViewById(R.id.tv_bottom);
        //初始化颜色数组
        colors = new int[]{Color.RED,Color.GREEN,Color.BLUE};
    }

    public void doSwitchColor(View view){
        //通过颜色数组切换颜色
        int temp = colors[0];
        for(int i = 0;i < colors.length - 1;i++){
            colors[i] = colors[i + 1];

        }
        colors[colors.length - 1] = temp;
        //根据颜色方案来设置三层标签背景
        tvBottom.setBackgroundColor(colors[0]);
        tvMiddle.setBackgroundColor(colors[1]);
        tvTOP.setBackgroundColor(colors[2]);

    }
}

在这里插入图片描述

演示
在这里插入图片描述

作业

自动切换颜色

package net.xh.switch_color;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView tvTop;
    private TextView tvMiddle;
    private TextView tvBottom;
    private boolean isRunning;
    private int[] colors;//颜色数组
    private Thread thread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取控件
        tvTop = findViewById(R.id.tv_top);
        tvMiddle = findViewById(R.id.tv_middle);
        tvBottom = findViewById(R.id.tv_bottom);
        //初始化颜色数组
        colors = new int[]{Color.RED,Color.GREEN,Color.BLUE};
    }

    public void doSwitchColor(View view){
        isRunning = true;
        MyThread myThread = new MyThread();
        thread = new Thread(myThread);

        thread.start();
    }
    class MyThread implements Runnable {

        @Override
        public void run() {
            while (isRunning) {
                //切换颜色
                int temp = colors[0];
                for (int i = 0; i < colors.length - 1; i++) {
                    colors[i] = colors[i + 1];
                }
                colors[colors.length - 1] = temp;

                tvBottom.setBackgroundColor(colors[0]);
                tvMiddle.setBackgroundColor(colors[1]);
                tvTop.setBackgroundColor(colors[2]);

                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }
    }

    public void doStop_color(View view){
        isRunning = false;
    }
}

作业

请添加图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值