【问题标题】:Android replace checkbox style with custom drawable selectorAndroid用自定义可绘制选择器替换复选框样式
【发布时间】:2015-09-19 22:22:24
【问题描述】:

我尝试使用以下内容制作 xml 选择器:

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

当我尝试将 backgroundDrawable 设置为 checkBox 时,复选框也不会替换 CheckBox 样式:

  shuffle.setBackground(android.support.v4.content.res.ResourcesCompat.getDrawable(getResources(), R.drawable.shuffle, null));

在这个问题之后:Change icons of checked and unchecked for Checkbox for Android 我需要用我的 xml 设置 button drawableandroid:button="@drawable/checkbox" 但我不能这样做,因为我正在以编程方式创建 CheckBox。 有没有办法做到这一点?

【问题讨论】:

  • 你尝试过 yourcheckbox.setButtonDrawable();
  • 我不知道有这样的命令,请将其发布为帮助他人的答案。
  • 你试过我的代码了吗

标签: android checkbox


【解决方案1】:

使用下面的代码行,我认为它会起作用

yourcheckbox.setButtonDrawable(yourdrawable); 

【讨论】:

    【解决方案2】:

    使用这个

    shuffle.setButtonDrawable(R.drawable.custom_checkbox_selector);
    

    XML 代码;

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

    【讨论】:

      【解决方案3】:

      简单的方法

      1. 在drawable文件夹中新建一个布局,命名为custom_checkbox(也可以重命名)

        <?xml version="1.0" encoding="utf-8"?>
        <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/Checked_image_name"android:state_checked="true"/>
        <item android:drawable="@drawable/Unchecked_image_name" android:state_checked="false"/>
        </selector>
        
      2. 在你的布局活动中使用它

        <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/custom_checkbox"/>
        

      【讨论】:

        【解决方案4】:

        这对我有用:

        <android.support.v7.widget.AppCompatCheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/custom_checkbox"
        android:button="@null"/>
        

        【讨论】:

          【解决方案5】:

          如果你使用 sdk 28 (androidx) 及以上版本尝试使用

          androidx.appcompat.widget.AppCompatCheckBox
          

          而不是

          Checkbox
          

          【讨论】:

          • 这对我有用。谢谢。 @Waran-
          【解决方案6】:

          drawable/checkbox_custome.xml

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

          【讨论】:

            猜你喜欢
            • 2015-08-10
            • 2017-01-21
            • 1970-01-01
            • 2011-06-07
            • 1970-01-01
            • 1970-01-01
            • 2023-02-17
            • 2021-01-27
            相关资源
            最近更新 更多