代码之家  ›  专栏  ›  技术社区  ›  Falmarri

自定义复选框图像Android

  •  165
  • Falmarri  · 技术社区  · 14 年前

    是否有一种简单的方法可以使用自定义图像作为复选框?我想复制Gmail的“明星”行为。所以我想要一个复选框,当选中时,它是一个填充星。如果不选中,则为空星。我是否必须使用ImageView并自己执行逻辑?

    8 回复  |  直到 6 年前
        1
  •  126
  •   Shashanth luckyhandler    8 年前

    复选框是按钮的子项,您只需给复选框一个具有多个状态的背景图像,如前所述。 here ,在“按钮样式”下:

    …并举例说明 here :

        2
  •  257
  •   Bojan Dević    8 年前

    创建可绘图复选框选择器:

    <?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" />
    
        3
  •  44
  •   WOUNDEDStevenJones    12 年前

    将btn_check.xml从android sdk/platforms/android-/data/res/drawable复制到项目的drawable文件夹中,并将“on”和“off”图像状态更改为自定义图像。

    那么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/..."

        4
  •  8
  •   Rahul    6 年前

    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
  •  5
  •   JJD    9 年前

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

    <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
  •  3
  •   Neo    9 年前

    试试看——

    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" />
    

    它能修复你所有的毛孔。

        7
  •  2
  •   androidevil    7 年前

    另一种选择是使用 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" />
    

    切换\u 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>
    

    ToGeLeTeX.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>
    
        8
  •  0
  •   mehdi    6 年前

    如果您使用的是自定义适配器,则 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>复选框\u 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>