您必须将值保存在某处,例如在数据库或共享首选项中。
在应用之前,setTheme(THEME);
您必须检索该值。
这是初始值:
public static int THEME = R.style.Theme_Sherlock;
首先设置值(例如“dark”)并重新启动应用程序:
DBAdapter db = new DBAdapter(this);
try {
db.open();
db.UpdateOption("theme", "dark");
}
catch (Exception ex) {}
finally {
db.close();
}
finish();
Intent intent = new Intent(this, ActionBarTabsPager.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
然后你得到新值并设置主题
DBAdapter db = new DBAdapter(this);
Cursor c = null;
try {
db.open();
c = db.GetOption(c, "theme");
String theme = c.getString(1);
if (theme.equalsIgnoreCase("dark")) {
THEME = R.style.Theme_Sherlock;
}
else if (theme.equalsIgnoreCase("light")) {
THEME = R.style.Theme_Sherlock_Light;
}
else if (theme.equalsIgnoreCase("darklight")) {
THEME = R.style.Theme_Sherlock_Light_DarkActionBar;
}
}
catch (Exception ex) {}
finally {
try {
if (c != null)
{
c.close();
c = null;
}
}
catch (Exception ex){}
db.close();
}
setTheme(THEME);
我有一个表选项来保存一些设置。当然,这也可以通过共享偏好来完成。