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);
     
     
  • 相关阅读:
    String类
    Scanner类
    Object类
    接口
    static关键字
    final关键字
    抽象类
    权限修饰符
    方法重写 (Override)
    面向对象思想特征
  • 原文地址:https://www.cnblogs.com/xing-star/p/11337378.html
Copyright © 2011-2022 走看看