我发现了许多动态改变活动主题的方法
但是我使用了一些不能改变主题的图表和图表,因为它们不仅仅是活动。
所以,我需要找到一种动态更改应用主题的方法,这样我的图表主题也会改变!
我想知道是否有人可以解决这个问题。
这是我的代码,适用于每项活动:
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);
答案 0 :(得分:0)
您可以将当前主题存储在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);