zoukankan      html  css  js  c++  java
  • FloatingActionButton动态更换背景色

    版权声明:本文为xing_star原创文章,转载请注明出处!

    本文同步自http://javaexception.com/archives/186

    FloatingActionButton 动态更换背景色

    最近碰到了个需求场景,需要动态切换FloatingActionButton的背景色
    先看下xml中的布局
    <android.support.design.widget.FloatingActionButton
    android:id=”@+id/fab_main_circle”
    android:layout_width=”wrap_content”
    android:layout_height=”wrap_content”
    android:src=”@drawable/ic_photo_album_white_48dp”
    app:fabSize=”normal”
    app:backgroundTint=”@color/colorPrimaryDark”/>
    FloatingActionButton的图片源是ic_photo_album_white_48dp ,这是一张纯色的图片,图片没有背景色,需要通过app:backgroundTint设置背景色。
     
    当我们的需求出现动态更改背景色时,就会碰到问题.
    ColorStateList colorStateList = ContextCompat.getColorStateList(getApplicationContext(), R.color.red);
    fabDownloadCircle.setBackgroundTintList(colorStateList);
    多次执行这段设置背景色的代码,会出现更改不了背景色,背景色始终保持在某一特定的色值。很是奇怪。
    google了一番,也没有找到合适的答案。
    最终到FloatingActionButton的源码里面,找到了一个api,setBackgroundTintMode。问题得到解决。
     
    完整的代码如下:
    ColorStateList colorStateList = ContextCompat.getColorStateList(getApplicationContext(), R.color.colorPrimaryDark);
    fabRandomCircle.setBackgroundTintMode(PorterDuff.Mode.SRC_ATOP);
    fabRandomCircle.setBackgroundTintList(colorStateList);
    ColorStateList colorStateList = ContextCompat.getColorStateList(getApplicationContext(), R.color.red;
    fabRandomCircle.setBackgroundTintMode(PorterDuff.Mode.SRC_ATOP);
    fabRandomCircle.setBackgroundTintList(colorStateList);
     
     
  • 相关阅读:
    cgo中调用有函数指针参数的C函数
    为什么要微服务架构服务化?
    学习刘杨人像后期笔记纪要
    错误日志表设计
    通过jstack日志分析和问题排查
    【JVM】jmap命令详解----查看JVM内存使用详情
    【JVM】jstat命令详解---JVM的统计监测工具
    介绍几种常用的监控工具
    【转】Pinpoint分布式系统性能监控工具
    【转】Skywalking概述
  • 原文地址:https://www.cnblogs.com/xing-star/p/11337378.html
Copyright © 2011-2022 走看看