使用drawable多次但颜色不同

时间:2016-09-06 19:29:09

标签: java android xml layer-list

我有一个选中器,用于选中和取消选中切换按钮 - 有没有一种方法可以使用带有形状的自定义图层列表并使用不同的颜色?我在运行时添加它们,但使用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 个答案:

答案 0 :(得分:1)

您可以将xml重用于图层列表Drawable,并以编程方式为每个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