Android4: 动态切换界面风格

Theme.Light:

Theme.Dark:


1. styles.xml定义两套theme


2. 点击Day/Night时

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

@Override public void onSaveInstanceState (Bundle outState) { super.onSaveInstanceState(outState); outState.putInt("theme", mThemeId); }
4. onCreate中根据theme id 加载theme

if(savedInstanceState != null) { if (savedInstanceState.getInt("theme", -1) != -1) { mThemeId = savedInstanceState.getInt("theme"); this.setTheme(mThemeId); } }








你可能感兴趣的:(Android4: 动态切换界面风格)