簡體   English   中英

動態更改整個應用程序主題

[英]Changing the whole application theme dynamically

我發現了許多動態更改活動主題的方法。
但是我使用了來自aChartengine的一些圖表和圖形,它們無法更改其主題,因為它們不僅類似於活動。
因此,我需要找到一種動態更改應用程序主題的方法,以便我的圖表主題也將發生變化!

我想知道是否有人可以解決這個問題。

這是我的代碼,適用於每個活動:

public class Utils
{
    private static int sTheme;

    public final static int THEME_DEFAULT = 0;
    public final static int THEME_WHITE = 1;
    public final static int THEME_BLUE = 2;
    public final static int THEME_PINK = 3;

    /**
     * Set the theme of the Activity, and restart it by creating a new Activity
     * of the same type.
     */
    public static void changeToTheme(Activity activity, int theme)
    {
        sTheme = theme;

        activity.finish();

        activity.startActivity(new Intent(activity, activity.getClass()));

    }

    /** Set the theme of the activity, according to the configuration. */
    public static void onActivityCreateSetTheme(Activity activity)
    {
        switch (sTheme)
        {
        default:
        case THEME_DEFAULT:
            break;
        case THEME_WHITE:
            activity.setTheme(R.style.Theme_white);
            break;
        case THEME_BLUE:
            Log.i("THM", "blue intered in utils");
            activity.setTheme(R.style.Theme_blue);
            break;
        case THEME_PINK:
            activity.setTheme(R.style.Theme_pink);

            break;
        }
    }
}

我使用以下代碼更改我的活動主題:

Utils.changeToTheme(G.tabActivity, Utils.THEME_DEFAULT);

您可以將當前主題存儲在SharedPreferences ,並在應用程序啟動時應用它:

SharedPreferences pref = context.getSharedPreferences("your_pref_name", android.content.Context.MODE_PRIVATE);
String theme = pref.getString("your_pref_key", "your_default_theme");
context.setTheme(theme);

然后,當您更改主題時,應重新啟動應用程序:

Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName());
finish();
startActivity(intent);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM