多次使用可绘制对象,但使用不同的颜色 [英] Use drawable multiple times but with different colours
问题描述
我有一个选中和未选中的切换按钮选择器-有没有一种方法可以使用具有形状的自定义图层列表并使用不同的颜色?我在运行时添加了它们,但使用XML作为设计.
I have a selector for a toggle button checked and unchecked - is there a way that I can use a custom layer list with a shape and use different colours? I am adding them in at runtime but using XML as the design.
<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
And my toggle_custom and 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.谢谢
So for example have one toggle that is using the colour red and another using blue using the same XML. Thanks
推荐答案
您可以将XML重新用于层列表Drawable
,并以编程方式为每个ToggleButton
创建StateListDrawable
,如下所示:
You can reuse the xml for the layer-list Drawable
and create the StateListDrawable
for each ToggleButton
programmatically like this:
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进行测试:
Let's test it with two 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));
这篇关于多次使用可绘制对象,但使用不同的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!