在xml中为按钮定义了自定义形状。现在,我想动态更改颜色。怎么样? [英] Defined custom shape for button in xml. Now I want to change the color dynamically. How?
本文介绍了在xml中为按钮定义了自定义形状。现在,我想动态更改颜色。怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个:
round_button.xml
<xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="oval">
<solid android:color="#dec60000"/>
<size android:width="150dp" android:height="150dp"/>
</shape>
</item>
<item android:state_pressed="false">
<shape android:shape="oval">
<solid android:color="#860000"/>
<size android:width="150dp" android:height="150dp"/>
</shape>
</item>
我的按钮:
<Button
android:id="@+id/incrementBTN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/round_button"
android:onClick="onClick"
android:soundEffectsEnabled="true"
android:text="0"
android:textSize="50sp"
tools:ignore="HardcodedText" />
动态地,我想更改背景颜色(在round_button xml中定义的)以编程方式。有办法吗?
Dynamically, I want to change the background color (which is defined in the round_button xml) programmatically. Is there a way I can do this?
推荐答案
我通过设置ColorFilter解决了它:
I solved it by setting a ColorFilter:
Drawable mDrawable = context.getResources().getDrawable(R.drawable.balloons);
mDrawable.setColorFilter(new PorterDuffColorFilter(0xffff00,PorterDuff.Mode.MULTIPLY));
myButton.setResource(mDrawable);
这篇关于在xml中为按钮定义了自定义形状。现在,我想动态更改颜色。怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文