尝试以编程方式创建 MaterialButton:
private val button = MaterialButton(context, null, R.style.Widget_MaterialComponents_Button)
但是在尝试渲染时出现以下错误:
Failed to find '@attr/textAppearanceButton' in current theme.
我试图传入一个使用我的 App 主题的上下文,该主题是从 .
"Theme.AppCompat.Light.NoActionBar"
初始化时尝试了多种样式,结果没有变化。
没有运气,任何想法我可能做错了什么?
您需要更新主题以从 MaterialComponents 主题之一继承,例如:
Theme.MaterialComponents.Light.NoActionBar
见https://material.io/develop/android/docs/getting-started
请注意,您应该能够通过单参数构造函数进行初始化:
private val button = MaterialButton(context)
前提是Context
您使用的是主题(无论是Activity
,还是Application
如果您在<application>
标签上设置了主题)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句