我正在尝试创建自定义视图并以编程方式设置其样式。我基于AppCompatButton创建了自定义视图:
public class RangeSelectorButton extends androidx.appcompat.widget.AppCompatButton {
public int onClickKey = -1;
public RangeSelectorButton(Context context) {
this(context, null);
}
public RangeSelectorButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public RangeSelectorButton(Context context, AttributeSet attrs) {
this(context, attrs, R.style.RangeSelectorButton);
}
}
现在我陷入了奇怪的行为:
<ru.SomeDomain.CustomViews.RangeSelector
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true" android:focusable="true">
<!-- In this case all works fine -->
<ru.SomeDomain.RangeSelectorButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="10dp"
android:minWidth="40dp"
style="@style/RangeSelectorButton"
/>
<!-- Style not applies -->
<ru.SomeDomain.CustomViews.RangeSelectorButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="10dp"
android:minWidth="40dp"
/>
</ru.SomeDomain.CustomViews.RangeSelector>
如果我不想在每次创建自定义视图时都在xml中使用style属性,该怎么办?
如果有必要,我的style.xml包含:
<style name="RangeSelectorButton" parent="@android:style/Widget.Button">
<item name="android:background">@drawable/range_toggle_button_selector</item>
</style>
range_toggle_button_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/range_unselected" android:state_pressed="false" />
<item android:drawable="@drawable/range_selected" android:state_pressed="true" />
</selector>
这里的问题是您要在defStyleAttr
参数的位置传递样式,该参数需要属性,而不是样式。这里有两个解决方案:
使用属性。在attrs.xml
中声明:
<attr name="rangeSelectorButtonStyle" format="reference"/>
并且在您的应用主题中:
<item name="rangeSelectorButtonStyle">@style/RangeSelectorButton</style>
并将您的构造函数更改为:
public RangeSelectorButton(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.rangeSelectorButtonStyle);
}
例如,如果您正在制作一个库,这是最好的方法,因为它允许用户自定义您的小部件样式。
如果您不想使用属性,则可以调用第四个View构造函数,该构造函数的参数采用默认样式而不是属性:
View(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
将此构造函数添加到视图中,并在第二个构造函数中,像这样调用第四个:
public RangeSelectorButton(Context context, AttributeSet attrs) {
this(context, attrs, 0, R.style.RangeSelectorButton);
}
请注意,第四个构造函数需要API 21。