Theme.Light:
Theme.Dark:
1. styles.xml定義兩套theme
[html]
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <style name="AppTheme.Light" parent="@Android:style/Theme.Holo.Light">
- <item name="menuIconToggleTitle">@drawable/ic_menu_toggle_title_holo_light</item>
- <item name="menuIconToggleTheme">@drawable/ic_menu_toggle_theme_holo_light</item>
- </style>
- <style name="AppTheme.Dark" parent="@android:style/Theme.Holo">
- <item name="menuIconToggleTitle">@drawable/ic_menu_toggle_title_holo_dark</item>
- <item name="menuIconToggleTheme">@drawable/ic_menu_toggle_theme_holo_dark</item>
- </style>
- </resources>
2. 點擊Day/Night時
[html]
- case R.id.menu_toggleTheme:
- if (mThemeId == R.style.AppTheme_Dark) {
- mThemeId = R.style.AppTheme_Light;
- } else {
- mThemeId = R.style.AppTheme_Dark;
- }
- this.recreate();
- return true;
3. theme id 保存為savedInstanceState
[html]
- @Override
- public void onSaveInstanceState (Bundle outState) {
- super.onSaveInstanceState(outState);
- outState.putInt("theme", mThemeId);
- }
4. onCreate中根據theme id 加載theme
[html]
- if(savedInstanceState != null) {
-
- if (savedInstanceState.getInt("theme", -1) != -1) {
- mThemeId = savedInstanceState.getInt("theme");
- this.setTheme(mThemeId);
- }
- }