美文网首页
Android切换主题

Android切换主题

作者: 蒙伟 | 来源:发表于2018-05-30 13:51 被阅读0次

    values-v21/styles.xml

    <resources>
    
        <!-- Base application theme. -->
        <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
        </style>
    
    
        <style name="Black" parent="android:Theme.Material">
            <item name="android:colorPrimaryDark">@color/status_grey</item>
            <item name="android:statusBarColor">@color/status_grey</item>
            <item name="android:colorPrimary">@color/action_grey</item>
            <item name="android:windowBackground">@color/window_bg_grey</item>
            <item name="android:navigationBarColor">@color/navigation_grey</item>
            <item name="android:colorForeground">@color/fg_grey</item>
            <item name="android:colorBackground">@color/bg_grey</item>
            <item name="android:colorAccent">@color/control_activated_grey</item>
            <item name="android:colorControlNormal">@color/control_normal_grey</item>
            <item name="android:colorControlHighlight">@color/control_highlight_grey</item>
            <item name="android:colorControlActivated">@color/control_activated_grey</item>
            <item name="android:colorButtonNormal">@color/button_normal_grey</item>
            <item name="android:textColor">@color/white_text</item>
            <item name="android:textColorPrimaryDisableOnly">@color/white_text</item>
            <item name="android:textColorPrimary">@color/white_text</item>
        </style>
    
        <style name="Blue" parent="android:Theme.Material">
            <item name="android:colorPrimaryDark">@color/status_blue</item>
            <item name="android:statusBarColor">@color/status_blue</item>
            <item name="android:colorPrimary">@color/action_blue</item>
            <item name="android:windowBackground">@color/window_bg_blue</item>
            <item name="android:navigationBarColor">@color/navigation_blue</item>
            <item name="android:colorForeground">@color/fg_blue</item>
            <item name="android:colorBackground">@color/bg_blue</item>
            <item name="android:colorAccent">@color/control_activated_blue</item>
            <item name="android:colorControlNormal">@color/control_normal_blue</item>
            <item name="android:colorControlHighlight">@color/control_highlight_blue</item>
            <item name="android:colorControlActivated">@color/control_activated_blue</item>
            <item name="android:colorButtonNormal">@color/button_normal_blue</item>
            <item name="android:textColor">@color/white_text</item>
            <item name="android:textColorPrimaryDisableOnly">@color/white_text</item>
            <item name="android:textColorPrimary">@color/white_text</item>
        </style>
    
        <style name="Green" parent="android:Theme.Material">
            <item name="android:colorPrimaryDark">@color/status_light_green</item>
            <item name="android:statusBarColor">@color/status_light_green</item>
            <item name="android:colorPrimary">@color/action_light_green</item>
            <item name="android:windowBackground">@color/window_bg_light_green</item>
            <item name="android:navigationBarColor">@color/navigation_light_green</item>
            <item name="android:colorForeground">@color/fg_light_green</item>
            <item name="android:colorBackground">@color/bg_light_green</item>
            <item name="android:colorAccent">@color/control_activated_light_green</item>
            <item name="android:colorControlNormal">@color/control_normal_light_green</item>
            <item name="android:colorControlHighlight">@color/control_highlight_light_green</item>
            <item name="android:colorControlActivated">@color/control_activated_light_green</item>
            <item name="android:colorButtonNormal">@color/button_normal_light_green</item>
            <item name="android:textColor">@color/white_text</item>
            <item name="android:textColorPrimaryDisableOnly">@color/white_text</item>
            <item name="android:textColorPrimary">@color/white_text</item>
        </style>
    
        <style name="Yellow" parent="android:Theme.Material">
            <item name="android:colorPrimaryDark">@color/status_lime</item>
            <item name="android:statusBarColor">@color/status_lime</item>
            <item name="android:colorPrimary">@color/action_lime</item>
            <item name="android:windowBackground">@color/window_bg_lime</item>
            <item name="android:navigationBarColor">@color/navigation_lime</item>
            <item name="android:colorForeground">@color/fg_lime</item>
            <item name="android:colorBackground">@color/bg_lime</item>
            <item name="android:colorAccent">@color/control_activated_lime</item>
            <item name="android:colorControlNormal">@color/control_normal_lime</item>
            <item name="android:colorControlHighlight">@color/control_highlight_lime</item>
            <item name="android:colorControlActivated">@color/control_activated_lime</item>
            <item name="android:colorButtonNormal">@color/button_normal_lime</item>
            <item name="android:textColor">@color/white_text</item>
            <item name="android:textColorPrimaryDisableOnly">@color/white_text</item>
            <item name="android:textColorPrimary">@color/white_text</item>
        </style>
    </resources>
    

    切换主题:

    package com.huatec.myapplication;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    
    public class MainActivity extends Activity {
    
        private static int theme = R.style.Blue;//设置默认主题
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setTheme(theme);//设置主题
            setContentView(R.layout.activity_main);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.change,menu);
            return true;
        }
    
        /**
         * 切换主题
         * @param item
         * @return
         */
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()){
                case R.id.blue://设置为蓝色主题
                    theme = R.style.Blue;
                    break;
                case R.id.gray://设置为黑色主题
                    theme = R.style.Black;
                    break;
                case R.id.green://设置为绿色主题
                    theme = R.style.Green;
                    break;
                case R.id.yellow://设置为黄色色主题
                    theme = R.style.Yellow;
                    break;
            }
            recreate();//重启界面
            return super.onOptionsItemSelected(item);
        }
    }
    

    效果图:


    2018-05-30 13_50_06.gif

    源码地址:https://github.com/280357392/Theme

    相关文章

      网友评论

          本文标题:Android切换主题

          本文链接:https://www.haomeiwen.com/subject/pritsftx.html