2010-10-19 96 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>