【问题标题】:Setting Custom-Theme to Button-Background programmatically以编程方式将自定义主题设置为按钮背景
【发布时间】:2020-01-30 15:15:07
【问题描述】:

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

我的主题.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>

第一次分配自定义主题的我的 Layout.xml:

        <Button
            android:id="@+id/buttonA2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="9dp"
            android:layout_marginLeft="9dp"
            android:layout_marginEnd="9dp"
            android:layout_marginRight="9dp"
            android:layout_marginBottom="16dp"
            **android:background="?button"**
            android:onClick="buttonA2"
            android:textColor="@color/white"
            app:layout_constraintBottom_toTopOf="@+id/buttonB2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

我将颜色更改为绿色的代码和重置对话框:

    public void buttonA2(View view) {
        if (currentQuestion.getOptb().equals(currentQuestion.getAnswer())) {
            **buttonA2.setBackground(getResources().getDrawable(R.drawable.button_correct));**
            if (qid < list.size() - 1) {
                disableButton();
                **correctDialog();**
            } else {
                gameWon();
            }
        } else {
            gameLostPlayAgain();
        }
    }

在正确的对话框中调用了resetColor():

    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));
    }

在我开始使用自定义主题之前,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(); 而不是Drawable buttonBack

【问题讨论】:

    标签: java android xml android-studio android-layout


    【解决方案1】:

    如果有人遇到同样的问题,我发现另一个问题给了我提示:https://stackoverflow.com/a/53884954/7671760

    使用typeArry buttonDraw我将默认按钮主题保存在onCreate --> buttonDraw = getTheme().obtainStyledAttributes(new 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)));
    

    不知道这是否是最聪明的方法,但它对我有用。

    干杯!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-13
      • 1970-01-01
      • 2013-09-27
      • 1970-01-01
      • 2023-03-02
      • 1970-01-01
      相关资源
      最近更新 更多