可能的重复:
如何在 Android 运行时更改当前主题 https://stackoverflow.com/questions/2482848/how-to-change-current-theme-at-runtime-in-android
我有一个 Android 应用程序,允许用户在运行时在主题之间切换。切换主题很容易,但是在重新创建活动之前不会应用主题。 https://developer.android.com/reference/android/view/ContextThemeWrapper.html#setTheme%28int%29我找到了一种方法将主题应用到当前活动 https://stackoverflow.com/questions/9446193/theme-programmatically-set-how-to-reload-activity-to-apply但如果用户按下后退按钮,之前的屏幕仍然具有旧主题。我如何更改这些活动的主题?支持它的应用程序示例:
我想只是一个提示:
Before finish();
Call
setResult(AnIntegerThatNotifiesThePreviousActivitiesToChangeTheme);
现在在您的所有活动中,实施 onActivityResult
protected void onActivityResult(int request, int result, Intent data) {
if(result == AnIntegerThatNotifiesThePreviousActivitiesToChangeTheme)
{
//update the current 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;
}
}
现在让您的所有活动扩展此主题活动:
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()
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)