您的位置:首页 > 移动开发 > Android开发

Android4: 动态切换界面风格

2012-02-11 15:16 316 查看
Theme.Light:



Theme.Dark:



1. styles.xml定义两套theme

<?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时

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);
            }
        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: