Android:自定义按钮上的背景颜色没有正确设置前的棒棒糖

当我使用一个标准的按钮

样式:

  match_parent @dimen/login_btn_height false @color/white @dimen/login_buttons_bottom_margin  

主题:

  @color/CrGreen @color/CrGreen @color/CrGreen  

一切工作正常,背景颜色设置在每个api级别16+

但是当我使用我的自定义按钮(用kotlin编写,需要自定义字体)

 open class CheckrobinButton : Button { val fontPath = "fonts/CoreSansG55.otf" constructor(context: Context) : super(context) { init() } constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet) { init() } constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int) : super(context, attributeSet, defStyleAttr) { init() } constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes){ init() } private fun init() { typeface = Typeface.createFromAsset(context.assets, fontPath) } } 

backgroundcolor只在API21 +(棒棒糖)设备上设置。 在棒棒糖设备上,按钮保持标准的灰色。

为什么?

如果您正在使用AppCompat ,则需要扩展AppCompatButton ,而不是Button如果您想在自定义类中使用Lollipop之前的自定义格式和颜色,请参阅Android支持库22.1博客文章中的说明 。