为什么禁用时Material FAB不会更改颜色? [英] Why does the Material FAB not change colors when disabled?

查看:87
本文介绍了为什么禁用时Material FAB不会更改颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在禁用材料浮动操作"按钮,但是当禁用设置为true时,颜色不会更改.我以为Material具有FAB的主题,并且在禁用时应变为浅灰色.我不想每次启用/禁用时都添加代码来更改背景.

I am disabling the Material Floating Action button but the color does not change when disabled is set to true. I thought Material has a theme for FAB's and when disabled it should turn light grey. I do not want to add code to change the background every time it is enabled/disabled.

我当前使用的是材料版本:1.1.0

I am currently on material version: 1.1.0

在代码中,我只是将fab设置为被fab.isEnabled = false

In the code I just set the fab to disabled by fab.isEnabled = false

这是xml

            <com.google.android.material.floatingactionbutton.FloatingActionButton
                android:id="@+id/save_reservation_fab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/keyline_2"
                app:backgroundTint="@color/color_primary"
                android:src="@drawable/ic_save_black_72dp"
                app:tint="@color/color_on_primary"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"/>

这是禁用/启用的晶圆厂的样子:

This is what the disabled/enabled fab looks like:

它应该看起来像这样:

推荐答案

怀疑,这是罪魁祸首:

app:backgroundTint="@color/color_primary"

这将着色FAB的颜色,而不管其状态如何.

This is going to tint the color of your FAB regardless of its state.

您可以通过将色调设置为ColorStateList而不是原始颜色值来解决此问题.也就是说,在res/color/目录中创建一个名为fab_color.xml的文件,并包括以下内容:

You could solve this by setting the tint to a ColorStateList instead of a raw color value. That is, create a file named fab_color.xml in your res/color/ directory, and include this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:color="your gray here"/>
    <item android:color="@color/color_primary"/>
</selector>

然后将您的颜色改为:

app:backgroundTint="@color/fab_color"

或者,您可以调整活动"的主题,以使FAB的默认颜色是所需的颜色(@color/color_primary),然后完全删除app:backgroundTint属性.

Alternatively, you could adjust your Activity's theme such that the default color of the FAB is the color you want (@color/color_primary) and then remove the app:backgroundTint attr altogether.

这篇关于为什么禁用时Material FAB不会更改颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆