gpt4 book ai didi

android - Android 5 上的 AppCompatButton : app:backgroundTint works but supportBackgroundTintList does NOT :(

转载 作者:行者123 更新时间:2023-11-29 18:36:43 25 4
gpt4 key购买 nike

我需要重用 XML 布局并以编程方式更改按钮颜色。在 Android 5 中,在 XML 中应用 app:backgroundTint 会更改按钮颜色,但我需要以编程方式执行此操作,并且我在 Recyclerview 中执行此操作:

holder.button.supportBackgroundTintList = ContextCompat.getColorStateList(context, backgroundColorRes)

这没有效果。

最佳答案

setSupportBackgroundTintList() 方法用 @RestrictTo({Scope.LIBRARY_GROUP}) 注释,这意味着您不应该直接调用它。相反,您应该使用 ViewCompat.setBackgroundTintList()

尝试将您的代码改为:

val colorStateList = ContextCompat.getColorStateList(context, backgroundColorRes)
ViewCompat.setBackgroundTintList(holder.button, colorStateList)

如果您查看 ViewCompat.setBackgroundTintList() 的源代码,您会发现它针对 API 21+(Android 5 及更高版本)执行的操作与早期版本不同。 “支持”背景色很可能只适用于早期版本的 Android,而 ViewCompat 会做到这一点,因此您不必考虑这一点。

关于android - Android 5 上的 AppCompatButton : app:backgroundTint works but supportBackgroundTintList does NOT :(,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54042168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com