博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
3.2 帧式布局
阅读量:37491 次
发布时间:2020-12-04

本文共 3816 字,大约阅读时间需要 12 分钟。

文章目录

一、导入新课

  • 有时候我们在设计安卓用户面时,有种需求,一个控件会层叠在另一个控件上,此时用线性布局就无法实现,就得使用一个新的布局——帧式布局。

二、新课概述

在这里插入图片描述

(一)帧式布局概述

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
    在这里插入图片描述

3、字符串资源文件

  • 字符串资源文件 - strings.xml
    在这里插入图片描述
帧式布局:切换颜色
顶层
中层
底层
切换颜色
  • 此时,查看界面预览效果
    在这里插入图片描述

4、主界面实现功能

  • 主界面类 - MainActivity

    在这里插入图片描述

  • 定义变量

    在这里插入图片描述

  • 通过资源标识符获取控件实例

    在这里插入图片描述

  • 编写切换颜色单击事件处理方法

    在这里插入图片描述

  • 查看完整代码

package net.hw.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 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); } /** * 切换颜色单击事件处理方法 * * @param view */ 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]); }}

5、启动应用,查看效果

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

6、优化切换颜色算法

  • 采用左移算法切换颜色

    在这里插入图片描述

  • 启动应用,查看效果

    在这里插入图片描述

  • 当切换颜色比较多,这个算法还得优化,采用循环结构来切换颜色

    在这里插入图片描述

  • 查看优化后的代码

package net.hw.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}; } /** * 切换颜色单击事件处理方法 * * @param view */ 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]); }}
  • 完成!

转载地址:http://bbpowy.baihongyu.com/

你可能感兴趣的文章
String s=new String(“abc“)创建了几个对象?
查看>>
【干货】Linux 网卡绑定的相关知识和技巧
查看>>
学习笔记2021-01-13
查看>>
soul源码学习-20210114
查看>>
编程语言介绍
查看>>
JVM 基础知识
查看>>
Java字节码(一)
查看>>
JVM 类加载器
查看>>
java内存模型(一)
查看>>
java内存模型(二)
查看>>
使用git上传远程的时候出现用户名密码错误 emote: Incorrect username or password ( access token )
查看>>
虚拟机ping不通主机,但是主机可以ping通虚拟机
查看>>
idea中创建maven文件时,maven路径是自己设置的
查看>>
Uncaught ReferenceError: $axure is not defined
查看>>
使用FFmpeg将pcm数据编码为aac
查看>>
使用FFmpeg将S16格式音频数据重采样为FLTP格式
查看>>
推荐系统实践学习系列(七)推荐系统实例
查看>>
推荐系统实践 学习总结
查看>>
Python3 Flask+nginx+Gunicorn部署(下)
查看>>
elasticsearch 出现 cluster_block_exception read_only_allow_delete问题
查看>>