AndroidASD完全解析03之FloatingActionButton

2024-01-19 23:08

本文主要是介绍AndroidASD完全解析03之FloatingActionButton,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前面我们介绍过了NavigationView和TextInputLayout这两个控件了,下面我们介绍第三个控件FloatingActionButton,这一篇也是ASD完全解析系列的第三篇,有兴趣的可以去看看前面两个控件的介绍:AndroidADS完全解析01之NavigationView AndroidADS完全解析02之TextInputLayout,好了,我们开始学习FloatingActionButton这个控件吧。

概述

这是官方对FloatingActionButton的描述:

Floating action buttons are used for a special type of promoted action. They are distinguished by a circled icon floating above the UI and have special motion behaviors related to morphing, launching, and the transferring anchor point.

Floating action buttons come in two sizes: the default and the mini. The size can be controlled with the fabSize attribute.

As this class descends from ImageView, you can control the icon which is displayed via setImageDrawable(Drawable).

The background color of this view defaults to the your theme's colorAccent. If you wish to change this at runtime then you can do so via setBackgroundTintList(ColorStateList).

简单的说FloatingActionButton(下面简称FAB)是Google提供的在界面上显示一个悬浮按钮,可以提供用户进行交互的。这个FAB的使用非常简单,但是,也要根据项目的需要进行使用,使用的恰当会给用户良好的体验,不恰当的话会破坏APP的使用体验,下面我们具体使用一下这个FAB吧。

使用

使用之前还是需要在bundle.gradle文件中添加如下代码:

compile 'com.android.support:design:24.0.0'

照例,我们来介绍一下FAB的提供的属性设置方法:

  • getBackgroundTintList()方法:如果指定有背景,返回一个可以绘制的背景色调

  • getBackgroundTintMode()方法:获取到背景色调模式

  • hide()方法:隐藏FAB

  • setBackground(Drawable)方法:设置背景图片

  • setBackgroundTintList(ColorStateList tint)方法:与getBackgroundTintList()对应

  • setBackgroundTintMode(PorterDuff.Mode tintMode)方法:与getBackgroundTintMode()方法对应

  • setRippleColor(int color)方法:设置ripple color

  • show()方法:显示FAB

XML属性的话跟ImageView差不多,不过有一个不同app:fabSize="",有两个值可以设置,分别是:normal和mini

使用的话就直接跟Imageview差不多,下面我们通过一个简单的例子体会一下吧:

首先是Activity代码:

package com.example.adsdemo;import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.view.View;/*** Created by Devin on 2016/8/15.*/
public class FABActivity extends AppCompatActivity {
private FloatingActionButton fab_show_1;
private FloatingActionButton fab_show_2;
private FloatingActionButton fab_show_3;@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_fab);fab_show_1 = (FloatingActionButton) findViewById(R.id.fab_show_1);fab_show_2 = (FloatingActionButton) findViewById(R.id.fab_show_2);fab_show_3 = (FloatingActionButton) findViewById(R.id.fab_show_3);fab_show_1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {ToastUtils.showToast(FABActivity.this, "点击的是第一个FAB");}});fab_show_2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {ToastUtils.showToast(FABActivity.this, "点击的是第二个FAB");}});fab_show_3.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {ToastUtils.showToast(FABActivity.this, "点击的是第三个FAB");}});
}
}

非常简单,就是给三个FAB设置一个点击事件而已,接着是XML布局文件代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:padding="16dp"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:padding="16dp"><android.support.design.widget.FloatingActionButtonandroid:id="@+id/fab_show_1"android:layout_width="wrap_content"android:layout_height="wrap_content"app:backgroundTint="@color/colorAccent"app:elevation="8dp"app:fabSize="normal"app:rippleColor="@color/colorPrimaryDark"/>
</RelativeLayout><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:padding="16dp"><android.support.design.widget.FloatingActionButtonandroid:id="@+id/fab_show_2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"app:backgroundTint="@color/colorAccent"app:elevation="8dp"app:fabSize="auto"app:rippleColor="@color/colorPrimaryDark"/>
</RelativeLayout><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:padding="16dp"><android.support.design.widget.FloatingActionButtonandroid:id="@+id/fab_show_3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"app:backgroundTint="@color/colorAccent"app:elevation="8dp"app:fabSize="mini"app:rippleColor="@color/colorPrimaryDark"/>
</RelativeLayout>
</LinearLayout>

在XML中,app:elevation属性是设置点击时的阴影效果,fabsize是设置大小。

然后是实现的效果图:

2267876-e0f6a3e9aff56eec.gif

FAB就简单介绍到这里了,我们在项目中使用这个的时候还是需要注意一下的,最后附上国内镜像API,猛戳这里

这篇关于AndroidASD完全解析03之FloatingActionButton的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/624001

相关文章

C语言中自动与强制转换全解析

《C语言中自动与强制转换全解析》在编写C程序时,类型转换是确保数据正确性和一致性的关键环节,无论是隐式转换还是显式转换,都各有特点和应用场景,本文将详细探讨C语言中的类型转换机制,帮助您更好地理解并在... 目录类型转换的重要性自动类型转换(隐式转换)强制类型转换(显式转换)常见错误与注意事项总结与建议类型

MySQL 缓存机制与架构解析(最新推荐)

《MySQL缓存机制与架构解析(最新推荐)》本文详细介绍了MySQL的缓存机制和整体架构,包括一级缓存(InnoDBBufferPool)和二级缓存(QueryCache),文章还探讨了SQL... 目录一、mysql缓存机制概述二、MySQL整体架构三、SQL查询执行全流程四、MySQL 8.0为何移除查

在Rust中要用Struct和Enum组织数据的原因解析

《在Rust中要用Struct和Enum组织数据的原因解析》在Rust中,Struct和Enum是组织数据的核心工具,Struct用于将相关字段封装为单一实体,便于管理和扩展,Enum用于明确定义所有... 目录为什么在Rust中要用Struct和Enum组织数据?一、使用struct组织数据:将相关字段绑

使用Java实现一个解析CURL脚本小工具

《使用Java实现一个解析CURL脚本小工具》文章介绍了如何使用Java实现一个解析CURL脚本的工具,该工具可以将CURL脚本中的Header解析为KVMap结构,获取URL路径、请求类型,解析UR... 目录使用示例实现原理具体实现CurlParserUtilCurlEntityICurlHandler

深入解析Spring TransactionTemplate 高级用法(示例代码)

《深入解析SpringTransactionTemplate高级用法(示例代码)》TransactionTemplate是Spring框架中一个强大的工具,它允许开发者以编程方式控制事务,通过... 目录1. TransactionTemplate 的核心概念2. 核心接口和类3. TransactionT

数据库使用之union、union all、各种join的用法区别解析

《数据库使用之union、unionall、各种join的用法区别解析》:本文主要介绍SQL中的Union和UnionAll的区别,包括去重与否以及使用时的注意事项,还详细解释了Join关键字,... 目录一、Union 和Union All1、区别:2、注意点:3、具体举例二、Join关键字的区别&php

Spring IOC控制反转的实现解析

《SpringIOC控制反转的实现解析》:本文主要介绍SpringIOC控制反转的实现,IOC是Spring的核心思想之一,它通过将对象的创建、依赖注入和生命周期管理交给容器来实现解耦,使开发者... 目录1. IOC的基本概念1.1 什么是IOC1.2 IOC与DI的关系2. IOC的设计目标3. IOC

java中的HashSet与 == 和 equals的区别示例解析

《java中的HashSet与==和equals的区别示例解析》HashSet是Java中基于哈希表实现的集合类,特点包括:元素唯一、无序和可包含null,本文给大家介绍java中的HashSe... 目录什么是HashSetHashSet 的主要特点是HashSet 的常用方法hasSet存储为啥是无序的

Linux中shell解析脚本的通配符、元字符、转义符说明

《Linux中shell解析脚本的通配符、元字符、转义符说明》:本文主要介绍shell通配符、元字符、转义符以及shell解析脚本的过程,通配符用于路径扩展,元字符用于多命令分割,转义符用于将特殊... 目录一、linux shell通配符(wildcard)二、shell元字符(特殊字符 Meta)三、s

使用Python实现批量访问URL并解析XML响应功能

《使用Python实现批量访问URL并解析XML响应功能》在现代Web开发和数据抓取中,批量访问URL并解析响应内容是一个常见的需求,本文将详细介绍如何使用Python实现批量访问URL并解析XML响... 目录引言1. 背景与需求2. 工具方法实现2.1 单URL访问与解析代码实现代码说明2.2 示例调用