damian
12/29/2016, 8:11 PMfun Context.wrapWithTheme(attrs: AttributeSet, defStyleAttr: Int = 0): Context {
val defAttrs = obtainStyledAttributes(attrs, android.support.v7.appcompat.R.styleable.View, defStyleAttr, 0)
try {
val themeResId = defAttrs.getResourceId(android.support.v7.appcompat.R.styleable.View_android_theme, -1)
return if (themeResId == -1) this else ContextThemeWrapper(this, themeResId)
} finally {
defAttrs.recycle()
}
}
class DefStyleThemeableButton : AppCompatButton {
constructor(context: Context)
: super(context)
constructor(context: Context, attrs: AttributeSet)
: super(context.wrapWithTheme(attrs, android.support.v7.appcompat.R.attr.buttonStyle), attrs)
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int)
: super(context.wrapWithTheme(attrs, defStyleAttr), attrs, defStyleAttr)
}