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。由于
它的工作原理!谢谢,非常感谢!只需将'Mode.MULTIPLY'更改为'Mode.SRC',因为我没有使用预设颜色,而是使用int值。 –