setBackgroundTintColorList(ColorStateList)之后的setImageResource(int)不起作用FloatingActionButton

时间:2018-03-31 12:51:57

标签: android android-layout floating-action-button

我必须在UtteranceProgressListener回调方法中更改FAB状态,但setBackgroundTintColorList()setBackgroundTintColorList()之后调用时不执行任何操作。但是,当我注释掉setBackgroundTintColorList(); setImageResource(); //doesn't work //setBackgroundTintColorList(); setImageResource(); //Now it works. 时,它会改变src图像。

reviewSentiment == 1;

出了什么问题。

2 个答案:

答案 0 :(得分:10)

我遇到了同样的问题-我猜这是一个错误。即使我的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

答案 1 :(得分:0)

只是一个附录:
在我的程序中,FAB是折叠式工具栏的一部分。几次尝试成功后,更改曾经失败的图标,最终以空FAB结束,这可能是由于Google图书馆中的比赛条件错误或状态错误所致。我首先删除了setBackgroundTintList(),但与给出的示例相反,此操作有效,即FAB仍然是彩色的,但为空。因此,我终于应用了ersin-ertan的出色解决方法,但是在Java中,是这样的:

    mFloatingButton.hide();
    mFloatingButton.setImageResource(...);
    mFloatingButton.setBackgroundTintList(...);
    mFloatingButton.show();
相关问题