【问题标题】:Material3 checkbox android cannot customize buttonMaterial3复选框android无法自定义按钮
【发布时间】:2022-10-25 21:04:50
【问题描述】:

如何在material3 android中自定义复选框的按钮?我试过这个:

//ic_checkbox.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/ic_checkbox_off_enabled"
        android:state_checked="false" />
    <item
        android:drawable="@drawable/checkbox_on_enabled2"
        android:state_checked="true" />
    <item
        android:drawable="@drawable/ic_checkbox_off_enabled" />
</selector>

//activity_main.xml
<CheckBox
    android:id="@+id/checkRememberMe"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="40dp"
    android:button="@drawable/ic_checkbox" />

checkbox_on_enabled2 :

checkbox_off_enabled :

但结果是我得到了空白复选框:

【问题讨论】:

    标签: android material3


    【解决方案1】:

    您偶然发现了在 Material 3 主题中配置的 &lt;item name="useMaterialThemeColors"&gt;false&lt;/item&gt;

    我遇到了类似的问题,我可以通过扩展我的应用程序主题并将扩展名应用于某些复选框来解决(请参阅此答案:https://stackoverflow.com/a/74176204/1535706; OP 也是我的)。

    【讨论】:

      【解决方案2】:

      CheckBox 绝对不是 Material Design Components 的一部分。

      而是使用MaterialCheckBox;例如:

      <com.google.android.material.checkbox.MaterialCheckBox
          style="@style/Widget.Material3.CompoundButton.CheckBox"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:checked="@{ selected }"/>
      

      【讨论】:

      • 也不起作用
      • “不起作用”不是错误描述,而是抱怨。
      • 我已经在帖子中描述了错误。我需要解决方案
      • 如前所述,使用com.google.android.material.checkbox.MaterialCheckBox。如果您愿意忽略这一点,那么您肯定是问题的一部分,而不是解决方案的一部分。当它“不起作用”时,如何将 Material Theme 分配给 Activity?
      • 这部分是无意义的:android:button="@drawable/ic_checkbox"。为什么你甚至在开始绘制自己的复选框时使用 Material Design?
      猜你喜欢
      • 2012-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多