[英]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.