我有这样的:在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" />
动态,我想改变背景颜色(这在ro中定义und_button xml)以编程方式。有什么办法可以做到这一点?
我相信不是。如果你改变颜色,它将取代背景。无论您想要更改哪种颜色,都必须使用不同颜色的第二种XML,并且您可以在Java –
@ th3pat3l Wow中将该XML更改为背景。 Bravo Google,使用您的SDK进行操作非常简单。 –
你想改变特定事件的背景颜色吗?例如,当按钮被点击时,当按钮被聚焦或者按钮被徘徊时?什么是实际使用? –