2010-10-19 80 views
139

有没有简单的方法来使用自定义图像的复选框?我正在寻找复制gmail的“主演”行为。所以我想要一个复选框,当选中时,它是一个充满活力的明星。而未经检查的是一颗空洞的明星。我必须使用imageview并自己做自己的逻辑吗?自定义复选框图像android

回答

112

复选框是按钮的孩子,您可以把您的复选框的背景图像与几个国家所描述的here下“按钮式”:

...并举例here

+25

谢谢,我居然发现我需要在这里HTTP正是://it-ride.blogspot。 com/2010/04/how-to-android-favorite-button-right.html但是如果我想要一个__real__自定义图像= P – Falmarri 2010-10-19 06:44:55

+2

谢谢。正是我一直在寻找的东西 - 我已经想出了整个状态的事情,但我一直在设置android:background而不是android:button,并以2个按钮结束。现在一切正常。 – 2010-11-23 23:07:06

+1

-1。下面的'android:button'解决方案比使用背景属性好得多! – 2012-09-18 20:05:14

42

复制btn_check。 xml from android-sdk/platforms/android - #/ data/res/drawable to your project's drawable folder and the'on'and'off'image states to your custom images。

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

+1

不良建议。图标可能会从版本更改为版本,并可能完全消失。如果你真的很喜欢默认图标,你可以从源头抓取它。 – 2012-10-13 07:51:50

+0

你是说直接通过“@android:drawable/...”引用默认图标是一个坏主意,或者这个过程完全是? – WOUNDEDStevenJones 2012-10-16 19:42:54

+1

示例:对全息图标的引用会使您的应用在预蜂窝设备上崩溃。维护和调试这样的麻烦真的很困难。所以我通常不仅复制xml,而且还要复制图片以确保找到资源。此外,确保用户界面在每台设备上都相同,这一点非常重要。 – 2012-10-18 06:47:05

5

如果你有Android的开源代码,你可以找到的样式定义下:
SRC /框架/碱/核心/ 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> 
221

创建绘制复选框选择:

<?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" /> 
+0

你在这个选择器中指定了什么记录?在您指定CheckBox的XML文件本身内? – 2014-03-27 20:14:09

+0

Tom:在可绘制文件夹中创建选择器并在布局文件夹中创建复选框 – 2014-03-28 06:50:43

+0

我必须在CheckBox中为'background'更改'button' – 2014-12-30 20:01:28

3

试试吧 -

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); 
    } 
} 

添加此ATTRIB -

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

它会修复所有的问题。

+0

它缺少'onSaveInstanceState()'和'onRestoreInstanceState()'方法,我认为checked状态会轮流失去 – EpicPandaForce 2015-12-02 13:23:20

2

另一种选择是使用空背景和自定义按钮的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>