问题
Possible Duplicate:
How to change current Theme at runtime in Android
I have an Android application where I allow users to switch between themes at runtime. Switching a theme is easy but the theme isn't applied until the activity is recreated. I found a way to apply the theme to current activity but if the user presses back button previous screens still have the old theme. How can I change theme for those activities? Example of app that supports it: Tasks Free
回答1:
Dynamically at runtime, call setTheme() in your activity's onCreate() method, before calling setContentView(). To change the theme, you simply need to restart your activity.
Please see this file..!
Also Want see this and this ... Hope this helps...!
回答2:
Just a hint I suppose:
Before finish();
Call
setResult(AnIntegerThatNotifiesThePreviousActivitiesToChangeTheme);
Now in all your Activities, implement onActivityResult
protected void onActivityResult(int request, int result, Intent data) {
if(result == AnIntegerThatNotifiesThePreviousActivitiesToChangeTheme)
{
//update the current theme
}
}
Another solution (Better):
Implement a class that saves the theme:
public class CurrentThemeHolder {
private CurrentThemeHolder() {
}
private static instance;
public static getInstance() {
if(instance == null)
return new CurrentThemeHolder();
else
return instance;
}
private int mTheme; //identifier of the theme
public getTheme() {
return mTheme;
}
public setTheme(int newTheme){
mTheme = newTheme;
}
}
Now let all ur activities extend this ThemeActivity:
public class ThemeActivity extends Activity {
private int mTheme;
protected void onResume() {
if(mTheme != CurrentThemeHolder.getInstance().getTheme()) {
//do what you should do to set the theme
mTheme = CurrentThemeHolder.getInstance().getTheme();
//everytime you set the theme save it
//this maybe should be done in onCreate()
}
}
}
来源:https://stackoverflow.com/questions/11421223/change-and-apply-theme-at-runtime-in-android