将您的 switch 更改为:
switch (cTheme)
{
case BLACK:
int myTheme = R.style.Default
activity.setTheme(myTheme);
//Save your activity theme color
saveTheme(myTheme);
break;
case YELLOW:
int myTheme = R.style.Green
activity.setTheme(myTheme);
//Save your activity theme color
saveTheme(myTheme);
break;
}
并将您的 onActivityCreateSetTheme(Activity activity) 更改为:
public static void onActivityCreateSetTheme(Activity activity, Int cTheme)
保存方法
public void saveTheme(int theme)
{
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
prefEditor.putInt("Theme",theme);
}
加载方法
public int loadTheme(){
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
//Load theme color
int theme = sharedPreferences.getInt("Theme",Color.RED); //RED is default color, when nothing is saved yet
return theme;
}
重要提示: 调用loadTheme() 之前 setContentView() 所以你的onCreate() 应该是这样的:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int theme = loadTheme(); //Load your theme here!!!!
CustomazationProcess.onActivityCreateSetTheme(this, theme);
setContentView(R.layout.something1);
findViewById(R.id.black).setOnClickListener(this);
findViewById(R.id.yellow).setOnClickListener(this);
}
希望对你有帮助