安卓学习笔记3.2 帧式布局


一、案例展示

1、创建安卓应用

  • 基于Empty Activity创建 - SwitchColor
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

2、字符串资源文件

  • strings.xml
    在这里插入图片描述
  • 代码
<resources>
    <string name="app_name">帧式布局:切换颜色</string>
    <string name="bottom">底层</string>
    <string name="middle">中层</string>
    <string name="top">顶层</string>
    <string name="switch_color">切换颜色</string>
</resources>

3、主布局资源文件

  • 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"
    tools:context=".MainActivity"
    android:gravity="center"
    android:orientation="vertical">

    <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"
            android:textColor="#ffff00"/>

        <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"
            android:textColor="#ffff00"/>

        <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"
            android:textColor="#ffff00"/>

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


</LinearLayout>
  • 打开Design视图,产看预览效果
    在这里插入图片描述

5、主界面

在这里插入图片描述

  • 定义以下变量
    在这里插入图片描述
  • 通过资源标识符(id)来获取控件的实例
    在这里插入图片描述
  • 绑定事件(在Button 控件中 通过onClick属性绑定事件doSwitchColor
    在这里插入图片描述

方法(一)

在这里插入图片描述

  • 代码
package net.hxl.switch_color;

import androidx.appcompat.app.AppCompatActivity;

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

import java.security.PublicKey;

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.hxl.switch_color;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
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]);
    }
}

方法(三)

在这里插入图片描述

  • 代码
package net.hxl.switch_color;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
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]);

        }
    }
}



6、最终效果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hxiug.虚晨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值