我有一个自定义按钮形状,用于所有按钮。我现在正在尝试实现主题,但我可以弄清楚如何自动更改按钮的“纯色”颜色而不摆脱我的自定义形状。谁能告诉我该怎么做?我尝试更改按钮的背景颜色,但没有看到任何变化;不知道为什么。
这是我的自定义按钮形状:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="14dp"
/>
<solid
android:color="#D1D1D1"
/>
<size
android:width="177dp"
android:height="60dp"
/>
<stroke
android:width="7dp"
android:color="#FFFFFF"
/>
</shape>
最佳答案
我认为你应该为 xml 文件分配一个 id
然后你只需通过findViewById来识别按钮
如果您使用多个主题,您可以使用大小写切换功能
Button button= (Button)findViewById(R.id.yourbutton)
button.setBackgroundColor(getResources().getColor(R.color.whatevercolor)); //or even better button.setBackgroundColor(0xFFFFFF);
关于java - 以编程方式更改自定义按钮形状的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39058790/