【发布时间】:2019-02-23 09:12:04
【问题描述】:
我正在尝试创建一个从 MaterialButton 扩展的自定义视图并在代码中应用样式,因此我不需要在 xml 中执行此操作。
class CustomRedButton @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : MaterialButton(ContextThemeWrapper(context, R.style.ButtonRedStyle), attrs, defStyleAttr)
样式是:
<style name="ButtonRedStyle"
parent="Widget.MaterialComponents.Button.TextButton">
<item name="backgroundTint">@color/red</item>
<item name="rippleColor">@color/grey</item>
<item name="strokeWidth">1dp</item>
<item name="strokeColor">@color/black</item>
</style>
一切正常,但 backgroundTint 属性。由于某种原因,背景颜色没有改变,它具有主题的原色。但是,如果我尝试将样式应用于 xml 中的 MaterialButton,它确实会改变颜色。
知道为什么会发生这种情况或如何实现吗?
【问题讨论】:
-
您使用的是哪个
AppTheme?如果是AppCompat主题,请尝试将其更改为MaterialComponents一次。 -
我正在使用
Theme.MaterialComponents.Light.NoActionBar
标签: android android-button android-theme android-styles material-components-android