【发布时间】:2020-04-18 13:59:26
【问题描述】:
我的自定义复选框 (MyCheckbox) 已从 androidx.appcompat.widget.AppCompatCheckBox 扩展,但默认样式不适用于它。
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
...
<item name="android:checkboxStyle">@style/CheckboxTheme</item>
<item name="checkboxStyle">@style/CheckboxTheme</item>
</style>
<style name="CheckboxTheme" parent="@style/Widget.AppCompat.CompoundButton.CheckBox">
<!-- for example try to change background -->
<item name="android:background">@color/colorPrimary</item>
</style>
但在预览和运行时没有改变任何东西。
另外,我迁移到 MaterialComponent(从 com.google.android.material.checkbox.MaterialCheckBox 扩展)版本:
com.google.android.material:material:1.1.0-beta01
并使用@style/Widget.MaterialComponents.CompoundButton.CheckBox 作为样式。但没有改变。
注意: 当我提到每个实例的样式时,这个问题就解决了:
<com.something.MyCheckBox
android:id="@+id/ch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="some thing"
style="@style/CheckboxTheme"
/>
但我想为此类的所有实例设置此样式。 你能帮帮我吗?
【问题讨论】:
-
你能确认
com.google.android.material:material:1.2.0-alpha03中存在这个问题吗? -
你确定吗?我刚刚对其进行了测试,并且视图的背景新是原色。甚至不需要在 xml 布局中添加样式标签。也许您在 v21 中添加此样式并在 api 上进行测试
-
没有。这是使用版本的一个错误,并在版本
1.2.0-alpha03中修复。谢谢@SinaDarvishi
标签: android android-custom-view androidx material-components material-components-android