2015-09-10 148 views
5

我已經閱讀了一些關於顏色的主題,但所有主題都必須通過style.xml進行設置。以編程方式更改colorControlActivated顏色

現在我用它來確定顏色。

<style name="Color1SwitchStyle"> 
    <item name="colorControlActivated">#0e8488</item> 
</style>' 

是否有可能改變SwitchCompat /複選框的顏色不使用代碼使用XML,比如?

+0

你對這個樣子? http://stackoverflow.com/a/27879897 – Sree

+0

是的,但我不知道如何通過代碼訪問ColorStateList。 –

+0

http://stackoverflow.com/a/17788095 – Sree

回答

31

其實,這並不難。

例子:

int[][] states = new int[][] { 
     new int[] {-android.R.attr.state_checked}, 
     new int[] {android.R.attr.state_checked}, 
}; 

int[] thumbColors = new int[] { 
     Color.BLACK, 
     Color.RED, 
}; 

int[] trackColors = new int[] { 
     Color.GREEN, 
     Color.BLUE, 
}; 

SwitchCompat switchCompat = (SwitchCompat) findViewById(R.id.switchControl); 
AppCompatCheckBox checkBox = (AppCompatCheckBox) findViewById(R.id.checkbox); 
checkBox.setSupportButtonTintList(new ColorStateList(states, thumbColors)); 
DrawableCompat.setTintList(DrawableCompat.wrap(switchCompat.getThumbDrawable()), new ColorStateList(states, thumbColors)); 
DrawableCompat.setTintList(DrawableCompat.wrap(switchCompat.getTrackDrawable()), new ColorStateList(states, trackColors)); 
+0

工程就像一個魅力! – enyciaa

相關問題