动态更改自定义Android主题

时间:2015-08-27 12:24:55

标签: java android xml mobile android-theme

我的style.xml文件中有一些客户主题。我想在单击alertDialog中的按钮时更改主题。

当我尝试更改主题时,我收到一个错误,即课程无法充气。我确认最低API至少为21,其他帖子必须完成。

这是我的代码:

style.xml     

<!-- Base application theme. -->
<style name="Theme.easter" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="color_1">@color/easter_color_1</item>
    <item name="color_2">@color/easter_color_2</item>
    <item name="color_3">@color/easter_color_3</item>
    <item name="color_4">@color/easter_color_4</item>
    <item name="color_5">@color/easter_color_5</item>
</style>

<style name="soft" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="color_1">@color/soft_color_1</item>
    <item name="color_2">@color/soft_color_2</item>
    <item name="color_3">@color/soft_color_3</item>
    <item name="color_4">@color/soft_color_4</item>
    <item name="color_5">@color/soft_color_5</item>
</style>

<style name="Theme.girl" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="color_1">@color/girl_color_1</item>
    <item name="color_2">@color/girl_color_2</item>
    <item name="color_3">@color/girl_color_3</item>
    <item name="color_4">@color/girl_color_4</item>
    <item name="color_5">@color/girl_color_5</item>
</style>

<style name="Theme.summer" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="color_1">@color/summer_color_1</item>
    <item name="color_2">@color/summer_color_2</item>
    <item name="color_3">@color/summer_color_3</item>
    <item name="color_4">@color/summer_color_4</item>
    <item name="color_5">@color/summer_color_5</item>
</style>

<style name="Theme.warm" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="color_1">@color/warm_color_1</item>
    <item name="color_2">@color/warm_color_2</item>
    <item name="color_3">@color/warm_color_3</item>
    <item name="color_4">@color/warm_color_4</item>
    <item name="color_5">@color/warm_color_5</item>
</style>

我正在尝试使用以下代码动态更改主题:

@Override
public void onClick(DialogInterface dialog, int item) {
                            switch (item){
                                case 0:
                                    themeUtils.changeToTheme(AdminSettings.this, themeUtils.EASTER);
                                    break;
                                case 1:
                                    themeUtils.changeToTheme(AdminSettings.this, themeUtils.SOFT);
                                    break;
                                case 2:
                                    themeUtils.changeToTheme(AdminSettings.this, themeUtils.GIRL);
                                    break;
                                case 3:
                                    themeUtils.changeToTheme(AdminSettings.this, themeUtils.SUMMER);
                                    break;
                                case 4:
                                    themeUtils.changeToTheme(AdminSettings.this, themeUtils.WARM);
                                    break;
                            }
                        }

themeUtils.class

package sourceCode;

import android.app.Activity;
import android.content.Intent;

import com.projects.fbgrecojr.vamoose.R;

/**
 * Created by fbgrecojr on 8/26/15.
 */
public class themeUtils {

private static int cTheme;

public final static int EASTER= 0;
public final static int SOFT = 1;
public final static int GIRL = 2;
public final static int SUMMER = 3;
public final static int WARM = 4;

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) {
        default:
        case EASTER:
            activity.setTheme(R.style.Theme_easter);
            break;
        case SOFT:
            activity.setTheme(R.style.soft);
            break;
        case GIRL:
            activity.setTheme(R.style.Theme_girl);
            break;
        case SUMMER:
            activity.setTheme(R.style.Theme_summer);
            break;
        case WARM:
            activity.setTheme(R.style.Theme_warm);
            break;

    }

}

}

最后,在我想要更改主题的活动中的onCreate方法中,我有这段代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        themeUtils.onActivityCreateSetTheme(this);
        setContentView(R.layout.activity_admin_settings);
....

我该如何解决这个问题?

0 个答案:

没有答案