设置背景颜色列表后,setImageResource(int) 在 FloatingActionButton 中无法生效。

8

在UtteranceProgressListener回调方法中,我需要更改FAB状态,但是当在setBackgroundTintColorList()之后调用setImageResource()时,它不起作用。然而,当我注释掉setBackgroundTintColorList()时,它确实会更改src图像。

setBackgroundTintColorList();
setImageResource();
//doesn't work

//setBackgroundTintColorList();
setImageResource();
//Now it works.

有什么问题吗?

2个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
14

我遇到了同样的问题——我猜这是个bug。尽管我的Fab是可见的,在设置之前和之后调用hide()show() 作为解决方法。

fun FloatingActionButton.changeFab(@ColorRes colorRes: Int, @DrawableRes imageRes: Int) {
    hide()
    backgroundTintList = ContextCompat.getColorStateList(context, colorRes)
    setImageResource(imageRes)
    show()
}

问题报告:https://issuetracker.google.com/issues/111316656


2
最初的回答: 仅作为补充说明:
在我的程序中,FAB是折叠工具栏的一部分。更改所使用的图标在一些成功尝试后会失败,并以空的FAB结束,可能是由于Google库中的竞争条件错误或状态错误。我首先删除了setBackgroundTintList(),但与给定的示例相反,这并没有起作用,即FAB保持着颜色,但是是空的。因此,最终我采用了ersin-ertan的优秀解决方法,但是用Java编写,看起来像这样:
    mFloatingButton.hide();
    mFloatingButton.setImageResource(...);
    mFloatingButton.setBackgroundTintList(...);
    mFloatingButton.show();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,