2016-09-06 152 views
2

我有一個切換按鈕的選擇器被選中和未選中 - 有沒有一種方法可以使用帶有形狀的自定義圖層列表並使用不同的顏色?我在運行時添加它們,但使用XML作爲設計。多次使用可繪製但具有不同顏色

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_checked="true" android:drawable="@drawable/toggle_custom"/> 
<item android:state_checked="false" android:drawable="@drawable/toggle_custom_off"/> 
</selector> 

而且我toggle_custom和toggle_custom_off

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:id="@+id/mainColourOn"> 
    <shape android:shape="rectangle" /> 
</item> 
<item android:bottom="10dp"> 
    <shape android:shape="rectangle"> 
     <solid android:color="#E6FFFFFF"/> 
    </shape> 
</item> 

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:id="@+id/mainColourOff"> 
    <shape android:shape="rectangle"/> 
</item> 
<item android:bottom="10dp"> 
    <shape android:shape="rectangle"> 
     <solid android:color="#FFF"/> 
    </shape> 
</item> 

 LayerDrawable layerOn = (LayerDrawable) getResources().getDrawable(R.drawable.toggle_custom, getTheme()); 

     LayerDrawable layerOff = (LayerDrawable) getResources().getDrawable(R.drawable.toggle_custom_off, getTheme()); 

     GradientDrawable toggleOn = (GradientDrawable) layerOn.findDrawableByLayerId(R.id.mainColourOn); 
     GradientDrawable toggleOff = (GradientDrawable) layerOff.findDrawableByLayerId(R.id.mainColourOff); 

     int colour = persons.get(i).getColour(); 

     toggleOn.mutate(); 
     toggleOff.mutate(); 
     toggleOn.setColor(colour); 
     toggleOff.setColor(colour); 

因此,例如,已經使用藍色使用一個切換正在使用紅色和另一相同的XML。由於

回答

1

可以重新使用該圖層列表Drawable XML並創建StateListDrawable每個ToggleButton程序是這樣的:

void setToggleButtonColor(ToggleButton tButton, int colour) 
{ 
    LayerDrawable layerOn = (LayerDrawable) ContextCompat.getDrawable(this, R.drawable.toggle_custom); 
    layerOn.mutate(); 
    LayerDrawable layerOff = (LayerDrawable) ContextCompat.getDrawable(this, R.drawable.toggle_custom_off); 
    layerOff.mutate(); 
    Drawable toggleOn = layerOn.findDrawableByLayerId(R.id.mainColourOn); 
    Drawable toggleOff = layerOff.findDrawableByLayerId(R.id.mainColourOff); 

    toggleOn.setColorFilter(colour, PorterDuff.Mode.MULTIPLY); 
    toggleOff.setColorFilter(colour, PorterDuff.Mode.MULTIPLY); 

    StateListDrawable tbBackground = new StateListDrawable(); 

    tbBackground.addState(new int[]{android.R.attr.state_checked}, layerOn); 
    tbBackground.addState(StateSet.WILD_CARD, layerOff); 

    tButton.setBackgroundDrawable(tbBackground); 

} 

讓我們兩個的ToggleButtons測試:

ToggleButton toggleButton1 = (ToggleButton) findViewById(R.id.togglebutton1); 
toggleButton1.setChecked(true); 

ToggleButton toggleButton2 = (ToggleButton) findViewById(R.id.togglebutton2); 
toggleButton2.setChecked(true); 

setToggleButtonColor(toggleButton1, ContextCompat.getColor(this, R.color.blue)); 
setToggleButtonColor(toggleButton2, ContextCompat.getColor(this, R.color.magenta)); 

enter image description here

+0

它的工作原理!謝謝,非常感謝!只需將'Mode.MULTIPLY'更改爲'Mode.SRC',因爲我沒有使用預設顏色,而是使用int值。 –

相關問題