Android FAB setBackgroundTintList with ColorStateList not working

ⅰ亾dé卋堺 提交于 2019-12-11 05:55:42

问题


I want to change the colour of the NFC scanning FAB when NFC is not enabled. I've managed to successfully change the colour when the app launches, but if the user taps the FAB and enables NFC, the colour isn't changed to the primary colour. The logs say it does, but the change doesn't happen.

My minSdkVersion = 15

XML:

<android.support.design.widget.FloatingActionButton
     android:id="@+id/fab_scan_nfc"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_marginLeft="10dp"
     app:backgroundTint="@color/colorPrimary"
     app:borderWidth="0dp"
     app:elevation="6dp"
     app:fabSize="mini"
     app:fab_colorNormal="@color/colorPrimary"
     app:fab_colorPressed="@color/colorPrimaryDark"
     app:fab_colorRipple="@color/colorAccent"
     app:srcCompat="@drawable/ic_nfc_n" />

Kotlin:

if (!isNFCEnabled) {
    Log.d(TAG, "Change NFC fab colour to disabled.")
    fab_scan_nfc.backgroundTintList = ColorStateList.valueOf(R.color.colorDisabled)
} else {
    Log.d(TAG, "Change NFC fab colour to primary.")
    fab_scan_nfc.backgroundTintList = ColorStateList.valueOf(R.color.colorPrimary)
}

This is the same as Java:

if (!isNFCEnabled) {
    Log.d(TAG, "Change NFC fab colour to disabled.")
    fab_scan_nfc.setBackgroundTintList = ColorStateList.valueOf(R.color.colorDisabled);
} else {
    Log.d(TAG, "Change NFC fab colour to primary.")
    fab_scan_nfc.setBackgroundTintList = ColorStateList.valueOf(R.color.colorPrimary);
}

As a sideline - when the code does apply the disabled colour background tint, there is this smaller circle on the FAB which looks like a touch indicator. Does this have something to do with using a colour state list ?

来源:https://stackoverflow.com/questions/44108455/android-fab-setbackgroundtintlist-with-colorstatelist-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!