У меня есть кнопка, которая расширяет MaterialButton
, и я пытаюсь получить доступ к пользовательским атрибутам, которые я определил в styles.xml
.Но TypedArray не содержит их
Вот мой пользовательский стиль
<style name="AppWidget.Button" parent="Widget.MaterialComponents.Button">
<item name="android:padding">@dimen/textview_horizontal_padding</item>
<item name="cornerRadius">24dp</item>
<item name="android:insetTop">0dp</item>
<item name="android:insetBottom">0dp</item>
<item name="android:textAppearance">@style/AppStyle.TextAppearance.Button</item>
<item name="android:textColor">@color/button_text</item>
<item name="backgroundTint">@color/button_bg</item>
<item name="strokeColor">@color/button_stroke</item>
<item name="strokeWidth">2dp</item>
<item name="progressWidth">20dp</item>
<item name="progressColor">@color/dph_teal</item>
</style>
Мои пользовательские атрибуты progressWidth
и progressColor
Вот мой attrs.xml
<declare-styleable name="Button">
<attr name="progressColor" format="color" />
<attr name="progressWidth" format="dimension" />
</declare-styleable>
В моей теме я устанавливаю
<item name="materialButtonStyle">@style/AppWidget.Button</item>
и как я пытаюсь их получить
private fun init(context: Context, attrs: AttributeSet?, defStyleAttr: Int) {
val ta = context.obtainStyledAttributes(attrs, R.styleable.Button, defStyleAttr, 0)
try {
val hasValue1 = ta.hasValue(R.styleable.Button_progressColor) //always false
val hasValue2 = ta.hasValue(R.styleable.Button_progressWidth) //always false
progressColor = ta.getColor(R.styleable.Button_progressColor, Color.WHITE)
progressWidth = ta.getDimension(R.styleable.Button_progressWidth, 5f)
} finally {
ta.recycle()
}
}
Есть идеи почему?Я в основном рисую индикатор прогресса в кнопке