【问题标题】:Custom checkbox image android自定义复选框图像android
【发布时间】:2011-04-27 07:12:56
【问题描述】:

是否有一种简单的方法可以为复选框使用自定义图像?我希望复制 gmail 的“加星标”行为。所以我想拥有一个复选框,当检查时,是一个填充的明星。未选中时是一个空星。我是否必须使用图像视图并自己做我自己的逻辑?

【问题讨论】:

    标签: android checkbox


    【解决方案1】:

    创建一个可绘制的复选框选择器:

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

    确保您的复选框是这样的android:button="@drawable/checkbox_selector"

    <CheckBox
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:button="@drawable/checkbox_selector"
        android:text="CheckBox"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/Black" />
    

    【讨论】:

    • 你在哪条记录中指定了这个选择器?在您指定 CheckBox 的 XML 文件本身中?
    • Tom:在可绘制文件夹中创建选择器,在布局文件夹中创建复选框
    • 我必须在复选框中将“按钮”更改为“背景”
    • 我已将我的项目升级到 android x 之后,我无法像您在 api 级别 21 之前所说的那样自定义我的复选框并且 android:button 不起作用。
    【解决方案2】:

    复选框是 Button 的子级,您可以在“按钮样式”下为复选框提供具有多种状态的背景图像,如 here 所述:

    ...并举例说明here:

    【讨论】:

    • 谢谢,我实际上在这里找到了我需要的东西 it-ride.blogspot.com/2010/04/… 但如果我想要一个真实自定义图像 =P
    • 谢谢。正是我一直在寻找的 - 我已经弄清楚了整个状态,但我一直在设置 android:background 而不是 android:button 并最终得到了 2 个按钮。现在一切正常。
    • -1。下面android:button的解决方案比使用背景属性好很多!
    • @Orabîg:这个反对意见是错误的。这个问题得到了完美的回答(“自定义复选框图像”)。存在此特定星号按钮的快捷方式这一事实并不会使此答案无效。
    • 虽然这可能是一个旧帖子,但想补充一点,Android Studio 也使用 android:button="@android:drawable/btn_star" 方法
    【解决方案3】:

    将 btn_check.xml 从 android-sdk/platforms/android-#/data/res/drawable 复制到项目的可绘制文件夹,并将“打开”和“关闭”图像状态更改为自定义图像。

    那么你的 xml 将只需要 android:button="@drawable/btn_check"

    <CheckBox
        android:button="@drawable/btn_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true" />
    

    如果你想使用不同的默认android图标,你可以使用android:button="@android:drawable/..."

    【讨论】:

    • 不好的建议。图标可能会因版本而异,并且可能会完全消失。如果你真的喜欢默认图标,你可以从源代码中获取它。
    • 你是说直接通过“@android:drawable/...”引用默认图标是个坏主意,还是整个过程?
    • 示例:对全息图标的引用会使您的应用在预蜂窝设备上崩溃。这样的麻烦,真的很难维护和调试。因此,我通常不仅复制 xml,还复制图像,以确保能够找到资源。此外,这对于确保 UI 在每台设备上看起来都一样非常重要。
    【解决方案4】:

    res/drawable/day_selector.xml

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

    res/layout/my_layout.xml

    <CheckBox
        android:id="@+id/check"
        android:layout_width="39dp"
        android:layout_height="39dp"
        android:background="@drawable/day_selector"
        android:button="@null"
        android:gravity="center"
        android:text="S"
        android:textColor="@color/black"
        android:textSize="12sp" />
    

    【讨论】:

    • 一些解释将帮助新用户了解您的代码如何解决问题。
    【解决方案5】:

    如果您有 Android 开源代码,您可以在以下位置找到样式定义:
    src/frameworks/base/core/res/res/values

    <style name="Widget.CompoundButton.CheckBox">
        <item name="android:background">
            @android:drawable/btn_check_label_background
        </item>
        <item name="android:button">
            ?android:attr/listChoiceIndicatorMultiple
        </item>
    </style>
    

    【讨论】:

      【解决方案6】:

      基于 Enselic 和 Rahul 的答案。

      它适用于我(在 API 21 之前和之后):

      <CheckBox
          android:id="@+id/checkbox"
          android:layout_width="40dp"
          android:layout_height="40dp"
          android:text=""
          android:gravity="center"
      
          android:background="@drawable/checkbox_selector"
          android:button="@null"
          app:buttonCompat="@null" />
      

      【讨论】:

        【解决方案7】:

        试试看 -

        package com;
        
        import android.content.Context;
        import android.content.res.TypedArray;
        import android.util.AttributeSet;
        import android.view.View;
        import android.widget.ImageView;
        
        
        
        public class CheckBoxImageView extends ImageView implements View.OnClickListener {
            boolean checked;
            int defImageRes;
            int checkedImageRes;
            OnCheckedChangeListener onCheckedChangeListener;
        
            public CheckBoxImageView(Context context, AttributeSet attr, int defStyle) {
                super(context, attr, defStyle);
                init(attr, defStyle);
            }
        
            public CheckBoxImageView(Context context, AttributeSet attr) {
                super(context, attr);
                init(attr, -1);
            }
        
            public CheckBoxImageView(Context context) {
                super(context);
            }
        
            public boolean isChecked() {
                return checked;
            }
        
            public void setChecked(boolean checked) {
                this.checked = checked;
                setImageResource(checked ? checkedImageRes : defImageRes);
            }
        
            private void init(AttributeSet attributeSet, int defStyle) {
                TypedArray a = null;
                if (defStyle != -1)
                    a = getContext().obtainStyledAttributes(attributeSet, R.styleable.CheckBoxImageView, defStyle, 0);
                else
                    a = getContext().obtainStyledAttributes(attributeSet, R.styleable.CheckBoxImageView);
                defImageRes = a.getResourceId(0, 0);
                checkedImageRes = a.getResourceId(1, 0);
                checked = a.getBoolean(2, false);
                a.recycle();
                setImageResource(checked ? checkedImageRes : defImageRes);
                setOnClickListener(this);
            }
        
            @Override
            public void onClick(View v) {
                checked = !checked;
                setImageResource(checked ? checkedImageRes : defImageRes);
                onCheckedChangeListener.onCheckedChanged(this, checked);
            }
        
            public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {
                this.onCheckedChangeListener = onCheckedChangeListener;
            }
        
            public static interface OnCheckedChangeListener {
                void onCheckedChanged(View buttonView, boolean isChecked);
            }
        }
        

        添加此属性 -

        <declare-styleable name="CheckBoxImageView">
                <attr name="default_img" format="integer"/>
                <attr name="checked_img" format="integer"/>
                <attr name="checked" format="boolean"/>
        </declare-styleable>
        

        使用喜欢 -

         <com.adonta.ziva.consumer.wrapper.CheckBoxImageView
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/checkBox"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:clickable="true"
                android:padding="5dp"
                app:checked_img="@drawable/check_box_checked"
                app:default_img="@drawable/check_box" />
        

        它会解决你所有的问题。

        【讨论】:

        • 它缺少onSaveInstanceState()onRestoreInstanceState() 方法,我认为检查状态会在轮换时丢失
        【解决方案8】:

        另一种选择是使用带有空背景和自定义按钮的ToggleButton

        下面是一个包含文本颜色选择器的示例。

        <ToggleButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/toggle_selector"
            android:background="@null"
            android:paddingLeft="10dp"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:textColor="@drawable/toggle_text"
            android:textOn="My on state"
            android:textOff="My off state" />
        

        toggle_selector.xml

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

        toggle_text.xml

        <?xml version="1.0" encoding="utf-8"?>
        <selector xmlns:android="http://schemas.android.com/apk/res/android">
        
            <item
                android:state_checked="true"
                android:color="@color/app_color" />
        
            <item
                android:color="@android:color/darker_gray" />
        
        </selector>
        

        【讨论】:

          【解决方案9】:

          如果您使用自定义适配器而不是 android:focusable="false"android:focusableInTouchMode="false" ,则必须在使用复选框时使列表项可点击。

          <CheckBox
                  android:id="@+id/checkbox_fav"
                  android:focusable="false"
                  android:focusableInTouchMode="false"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:button="@drawable/checkbox_layout"/>
          

          drawable>checkbox_layout.xml

          <selector xmlns:android="http://schemas.android.com/apk/res/android" >
              <item android:drawable="@drawable/uncked_checkbox"
                  android:state_checked="false"/>
              <item android:drawable="@drawable/selected_checkbox"
                  android:state_checked="true"/>
              <item android:drawable="@drawable/uncked_checkbox"/>
          </selector>
          

          【讨论】:

            【解决方案10】:

            如果您使用 androidx.appcompat:appcompat 并希望自定义可绘制对象(类型为 selectorandroid:state_checked)在旧平台版本和新平台版本上工作,您需要使用

                <CheckBox
                    app:buttonCompat="@drawable/..."
            

            而不是

                <CheckBox
                    android:button="@drawable/..."
            

            【讨论】:

              【解决方案11】:

              在 Material Checkbox version-1.3.0 的 android:button 中添加自定义 drawable 对我不起作用。我不得不设置 android:drawable="@drawable/checkbox_selector" 并设置 android:button="@null" 。您还可以添加android:drawablePadding 使其看起来不错。但是,这会使整个复选框(连同文本)都可以点击。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2014-06-01
                • 1970-01-01
                • 2011-06-15
                • 1970-01-01
                • 2013-04-11
                • 1970-01-01
                相关资源
                最近更新 更多