Java 以编程方式将自定义主题设置为按钮背景

Java 以编程方式将自定义主题设置为按钮背景,java,android,xml,android-studio,android-layout,Java,Android,Xml,Android Studio,Android Layout,我已经创建了一些自定义主题,它们可以帮助我根据用户偏好自定义布局。。在我的应用程序(测验应用程序)中,如果答案正确,我将更改按钮的颜色。之后,我显示了一个“correctAnswer”-对话框,其中有一个onClick按钮,按钮应该被重置并重新获得其正常颜色。但由于我使用的是自定义主题,所以无法实现这一点。欢迎任何帮助 My theme.xml: <style name="Theme.Custom1" parent="Theme.AppCompat.NoActionBar">

我已经创建了一些自定义主题,它们可以帮助我根据用户偏好自定义布局。。在我的应用程序(测验应用程序)中,如果答案正确,我将更改按钮的颜色。之后,我显示了一个“correctAnswer”-对话框,其中有一个onClick按钮,按钮应该被重置并重新获得其正常颜色。但由于我使用的是自定义主题,所以无法实现这一点。欢迎任何帮助

My theme.xml:

    <style name="Theme.Custom1" parent="Theme.AppCompat.NoActionBar">
        <item name="background">@color/Red</item>
        <item name="arrow">@color/RedDark</item>
        <item name="toolbar">@color/RedDark</item>
        <item name="text">@color/Gold</item>
        <item name="button">@drawable/button_custom1</item>
    </style>
在我开始使用自定义主题之前,resetColor就起作用了。。现在我在用户偏好上设置了4个不同的主题,不仅有一个可绘制的“button_custom”,还有4个(button_custom1到4)。长话短说:在resetColor中,我需要知道按钮上使用了哪个自定义主题,然后将颜色更改为绿色,并再次将背景设置为该自定义主题(例如,自定义主题内的可绘制主题)

非常感谢

编辑: 我已经尝试过类似的东西,但没有任何有用的结果:

Drawable buttonBack = buttonA2.getBackground();
String buttonBackground = buttonBack.toString();
System.out.println(buttonBackground);
int i = getResources().getIdentifier("tb.quiz:drawable/" + buttonBackground, null, null);
System.out.println(i);
buttonA2.setBackground(getResources().getDrawable(i));


另一次尝试是使用
Resources.Theme buttonBack=this.getTheme()
如果有人遇到同样的问题,我发现另一个问题给了我提示,而不是
可拉拔按钮背面

使用
typeArry buttonDraw
我将默认按钮主题保存在
onCreate
-->

buttonDraw=getTheme().ActainStyledAttributes(新int[]{R.attr.button})

然后将我的
resetColor()
更改为:

buttonA2.setBackground(getResources().getDrawable(buttonDraw.getResourceId(0, 1)));
buttonB2.setBackground(getResources().getDrawable(buttonDraw.getResourceId(0, 1)));
buttonC2.setBackground(getResources().getDrawable(buttonDraw.getResourceId(0, 1)));
buttonD2.setBackground(getResources().getDrawable(buttonDraw.getResourceId(0, 1)));
不知道这是不是最聪明的方法,但对我来说很有效

干杯

    public void resetColor() {
        buttonA2.setBackground(getResources().getDrawable(R.drawable.button_custom));
        buttonB2.setBackground(getResources().getDrawable(R.drawable.button_custom));
        buttonC2.setBackground(getResources().getDrawable(R.drawable.button_custom));
        buttonD2.setBackground(getResources().getDrawable(R.drawable.button_custom));
    }
Drawable buttonBack = buttonA2.getBackground();
String buttonBackground = buttonBack.toString();
System.out.println(buttonBackground);
int i = getResources().getIdentifier("tb.quiz:drawable/" + buttonBackground, null, null);
System.out.println(i);
buttonA2.setBackground(getResources().getDrawable(i));

buttonA2.setBackground(getResources().getDrawable(buttonDraw.getResourceId(0, 1)));
buttonB2.setBackground(getResources().getDrawable(buttonDraw.getResourceId(0, 1)));
buttonC2.setBackground(getResources().getDrawable(buttonDraw.getResourceId(0, 1)));
buttonD2.setBackground(getResources().getDrawable(buttonDraw.getResourceId(0, 1)));