2013-12-12 38 views
15

我正在開發android應用程序因爲我使用複選框,但默認複選框刻度顏色是藍色,所以我想要將該顏色更改爲黃色。是否有任何內置屬性將顏色設置爲複選框打勾。如何更改android中的複選框刻度顏色

+2

這是解決方案:[檢查出來](http://stackoverflow.com/questions/3192173/change-icons-of-checked-and-unchecked-for-checkbox-for-android) –

回答

30

不幸的是,改變checkob對勾的顏色是不是一個簡單的屬性

創建名爲cb_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/checked" /> 
    <item android:state_checked="false" android:drawable="@drawable/unchecked" /> 
</selector> 

在res \繪項目\文件夾中選擇XML文件中的佈局文件將此文件應用於您的複選框

<CheckBox 
    android:id="@+id/cb" 
    android:text="My CheckBox" 
    android:button="@drawable/cb_selector" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 

在drawables文件夾中添加unchecked.png和checked.png。這些是複選框的選中和未選中的圖像。

+6

這使我的複選框消失O_O –

+0

您將不得不在邊框周圍顯示圖像,一個圖像將爲空邊框,另一個圖像爲刻度圖像 –

+0

消失是因爲您使用了android中的顏色更改:drawable =「@ drawable/checked」。你需要圖像! – ketom

0

使用自定義選擇器作爲複選框。

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:drawable="@drawable/patch_pressed" android:state_pressed="true"/> 
    <item android:drawable="@drawable/patch_normal" android:state_enabled="true"/> 
    <item android:drawable="@drawable/patchdisable" android:state_enabled="false"/> 


</selector> 

像這樣。

1

轉到styles.xml並添加此行。利用這一點,你

<style> 
<item name="colorAccent">@android:color/holo_green_dark</item> 
</style> 

可以改變顏色或設置不同的顏色

+0

這段代碼將嘗試...它工作正常 –

+2

seriosly您的評論工作正常,但你有什麼寫在你的答案 – Shekhar

+1

不會改變你的主題改變方式不僅僅是複選框? –

2

由於API 21的你可以用按鈕着色屬性

android:buttonTint="#FFFF00" 
40

你可以做到這一點不改變使用buttonTint繪製(從API 23開始):

<CheckBox 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:buttonTint="@color/CHECKMARK_COLOR_HERE" /> 

或使用AppCombatCheckBox for ol Android版本的兼容性。

<android.support.v7.widget.AppCompatCheckBox 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    app:buttonTint="@color/CHECKMARK_COLOR_HERE" /> 
+1

嗨,@afathman這是一個簡單的解決方案 – Cloy

+0

IMO這一個是這裏最好的答案 – fragon

+1

當使用starStyle時,它設置顏色爲選中狀態和未選中狀態,所以有點擊敗目的。 – yumoji

8

您可以從android.support.v7庫使用AppCompatCheckBox的屬性app:buttonTint

<android.support.v7.widget.AppCompatCheckBox 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:buttonTint="@color/colorAccent"/> 

優點:也可以在API 21下工作,您不需要重畫複選框。

相關問題