动态更改所有按钮的样式

时间:2014-12-30 05:46:54

标签: android android-styles

我在values-> styles.xml中声明了所有按钮的样式,就像这样。

 <style name="BlueButton" parent="android:style/Widget.Button">
        <item name="android:background">@color/blue</item>
 </style>

在整个主题中

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:buttonStyle">@style/BlueButton</item>
</style>

我想根据用户选择的颜色更改应用程序中所有按钮的颜色。有没有办法在styles.xml中定义不同的样式并动态地将它们分配给按钮。

请注意,我想一次更改应用程序中所有按钮的颜色,而不是逐个更改。例如,假设当前我的所有按钮都是黑色的,当用户选择红色主题时,我希望应用程序中的所有按钮都有红色背景。

3 个答案:

答案 0 :(得分:0)

Hi try this code. 

          ThemeUtils class:

            public class ThemeUtils {
            private static int cTheme;
            public final static int RED = 0;
            public final static int GREEN = 1;

            public static void changeToTheme(Activity activity, int theme) {
                cTheme = theme;
                activity.finish();
                activity.startActivity(new Intent(activity, activity.getClass()));
            }

            public static void onActivityCreateSetTheme(Activity activity) {
                switch (cTheme) {
                case BLUE:
                    activity.setTheme(R.style.AppBaseTheme);
                    break;

                default:
                }
            }
        }

        And to set theme use this code:

                ThemeUtils.changeToTheme(this, ThemeUtils.BLUE);

        If you want to create custom theme in android see this link: 
         http://romannurik.github.io/AndroidAssetStudio/

答案 1 :(得分:0)

尝试使用主题概念。

  • 首先创建按钮的属性

<attr format="reference" name="button_style"/>

  • 然后根据需要创建样式:

<style name="BlueButton" parent="android:style/Widget.Button"> <item name="android:background">@color/blue</item> </style>

  • 然后申请主题:

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="button_style">@style/BlueButton</item> </style>

  • 最后将此应用于Layout xml
  • 中的按钮

<Button style="?button_style" />

然后,如果您想要更改其他主题,请创建相同的(2点和3点),它将自动应用于您的按钮

答案 2 :(得分:0)

您可以通过编程方式更改按钮背景。是的,只在每个Activity中调用此方法一次,例如在Activity中为manifest.xml声明主题。一个colordrawable资源适用于Activity中的所有按钮。所以,按照我的步骤:

创建新的第三方类

在这里,我使用ButtonBackground命名此类:

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.Button;

public class ButtonBackground {

    /* This method is used to change the background with "drawable" resource. Maximum
     * button to change its background is 5, no more. It caused by button's parameter
     * only available for 5. You can add new parameter (button6, button 7, etc) with "int" type if needed.
     */
    public void setButtonBackgroundDrawable(Context context, Activity activity, int drawable,
            int button1, int button2, int button3, int button4, int button5) {

        Drawable backgroundDrawable = context.getResources()
                .getDrawable(drawable);
        Button a = (Button) activity.findViewById(button1);
        Button b = (Button) activity.findViewById(button2);
        Button c = (Button) activity.findViewById(button3);
        Button d = (Button) activity.findViewById(button4);
        Button e = (Button) activity.findViewById(button5);

        if (button1 != 0) {
            a.setBackground(backgroundDrawable);
        }
        if (button2 != 0) {
            b.setBackground(backgroundDrawable);
        }
        if (button3 != 0) {
            c.setBackground(backgroundDrawable);
        }
        if (button4 != 0) {
            d.setBackground(backgroundDrawable);
        }
        if (button5 != 0) {
            e.setBackground(backgroundDrawable);
        }
    }

    /* This method is used to change the background with "color" resource. Maximum
     * button to change its background is 5, no more. It caused by button's parameter
     * only available for 5. You can add new parameter (button6, button 7, etc) with "int" type if needed.
     */
    public void setButtonBackgroundColor(Context context, Activity activity, int color,
            int button1, int button2, int button3, int button4, int button5) {

        int backgroundColor = context.getResources().getColor(color);
        Button a = (Button) activity.findViewById(button1);
        Button b = (Button) activity.findViewById(button2);
        Button c = (Button) activity.findViewById(button3);
        Button d = (Button) activity.findViewById(button4);
        Button e = (Button) activity.findViewById(button5);

        if (button1 != 0) {
            a.setBackgroundColor(backgroundColor);
        }
        if (button2 != 0) {
            b.setBackgroundColor(backgroundColor);
        }
        if (button3 != 0) {
            c.setBackgroundColor(backgroundColor);
        }
        if (button4 != 0) {
            d.setBackgroundColor(backgroundColor);
        }
        if (button5 != 0) {
            e.setBackgroundColor(backgroundColor);
        }
    }
}

如何使用?

例如,我有3个按钮,我想使用drawable资源更改其背景。它们是buttonId1buttonId2buttonId3。我的drawable名称为background_button_drawable.xml,我当前的Activity名称为MainActivity。所以我在onCreate()方法中编写了下面的代码:

ButtonBackground buttonBackground = new ButtonBackground();
buttonBackground.setButtonBackgroundDrawable(getApplicationContext(), MainActivity.this,
            R.drawable.background_button_drawable, R.id.buttonId1, R.id.buttonId2, R.id.buttonId3, 0, 0);

注意:

上面的代码中有两个0。这意味着button4button5在参数中为空。因此,如果按钮为空或参数过多,请提供0值。

如果我有6个按钮怎么办?

您需要再次使用一个参数,并且必须添加新参数(button6)。参数必须为int类型:

public void setButtonBackgroundDrawable(... , int button6) {

并将其添加到内部:

Button f = (Button) activity.findViewById(button6);

if (button6 != 0) {
    f.setBackground(backgroundDrawable);
}

所以当你使用它时:

ButtonBackground buttonBackground = new ButtonBackground();
buttonBackground.setButtonBackgroundDrawable(getApplicationContext(), MainActivity.this,
            R.drawable.background_button_drawable, R.id.buttonId1, R.id.buttonId2, R.id.buttonId3, buttonId4, buttonId5, buttonId6);

如何使用color资源更改其背景?

您需要使用setButtonBackgroundDrawable()更改setButtonBackgroundColor()方法。它看起来像这样:

ButtonBackground buttonBackground = new ButtonBackground();
buttonBackground.setButtonBackgroundColor(getApplicationContext(), MainActivity.this,
                R.color.background_button_blue, R.id.buttonId1, R.id.buttonId2, R.id.buttonId3, 0, 0);

使用起来非常简单,不是吗?