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

海夫

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

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

海夫

如果有人遇到同样的问题,我发现了另一个给我提示的问题: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)));

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

干杯!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何以编程方式将自定义图像设置为 UIBarButtonItem

将自定义chipDrawable背景设置为Chip

android通过编程将自定义值设置为自定义textview

以编程方式将自定义按钮添加到具有约束的视图

如何在altair中将自定义颜色主题设置为默认值?

如何在代码后面将自定义背景设置为ListView项目颜色?

Obj-C - 使用自定义 int 值以编程方式设置 imageview 背景颜色?

如何在Android中以编程方式在圆角的imageview中设置自定义背景色

以编程方式为Google协作平台页面设置自定义权限

将自定义域设置为“主要”的好处

将自定义 ActionBar 重力设置为 RIGHT

将自定义字体设置为微调器

在OpenCv中将自定义模型设置为CvSVM

Android:将自定义值设置为NumberPicker

如何将自定义委托设置为UITableViewController?

以编程方式更改自定义按钮形状的颜色

以编程方式自定义 WPF 按钮样式

如何以编程方式设置自定义TAlphaColor?

Swift 4以编程方式设置自定义字体

如何设置自定义类以编程方式创建UIButton?

在Woocommerce 3中以编程方式设置自定义运费

如何将自定义标记设置为自定义视图?

如何将自定义tabBar设置为我的自定义TabBarController?

将自定义FXML属性设置为自定义javafx组件的参数

将自定义样式设置为android中的自定义视图内的textviews

以编程方式将自定义视图放入ScrollView(或HorizontalScrollView)中(无滚动)

如何以编程方式将自定义布局添加到 StackView 或 TableView?

以编程方式将自定义视图添加到片段布局

以编程方式将自定义范围添加到 Azure B2C