Android 主题切换/换肤方案 研究(三) - 开源中国

3.开源中国

开源中国客户端在侧滑菜单栏里有日间/夜间模式切换按钮。

分析源码,

查看MainActivity的onCreate()方法:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (AppContext.getNightModeSwitch()) {
            setTheme(R.style.AppBaseTheme_Night);
        } else {
            setTheme(R.style.AppBaseTheme_Light);
        }

        ......
    }

上诉代码是在Activit启动的时候读取之前选择的主题模式,然后设置主题。

可以看到也是通过在styles.xml文件里设置主题样式来切换主题的,来看styles.xml文件:

  <!-- 日间模式 -->
    <style name="AppBaseTheme_Light" parent="@style/Theme.AppCompat.Light">
        <item name="colorPrimary">@color/day_colorPrimary</item>
        <item name="colorPrimaryDark">@color/day_colorPrimaryDark</item>
        <item name="android:textColorLink">#0b9a27</item>
        <item name="android:textColorPrimary">@color/white</item>
        <item name="editTextColor">@color/gray</item>
        <item name="actionBarPopupTheme">@style/ThemeOverlay.AppCompat.Dark</item>
        <item name="android:windowBackground">@color/window_background</item>
        <item name="actionMenuTextColor">@color/white</item>
        <item name="textColor">@color/day_textColor</item>
        <item name="infoTextColor">@color/day_infoTextColor</item>
        <item name="layout_bg_normal">@color/day_layout_bg_normal</item>
        <item name="lineColor">@color/day_line_color</item>
        <item name="windows_bg">@color/day_windows_bg</item>
        <item name="sliding_block_bg">@drawable/image_sliding_block</item>
        <item name="layout_item_bg">@drawable/day_list_item_background</item>
        <item name="android:editTextColor">@color/day_edit_color</item>
        <item name="android:textColorHint">@color/day_edit_hit_color</item>
        <item name="comment_reply_container_bg">@drawable/day_comment_reply_container_bg</item>
        <item name="message_bg_left">@drawable/chat_from_bg_normal</item>
        <item name="message_bg_right">@drawable/chat_to_bg_normal</item>

        <item name="quickoption_icon_text_selector">@drawable/day_quickoption_icon_text_selector</item>
        <item name="quickoption_icon_album_selector">@drawable/day_quickoption_icon_album_selector</item>
        <item name="quickoption_icon_photo_selector">@drawable/day_quickoption_icon_photo_selector</item>
        <item name="quickoption_icon_voice_selector">@drawable/day_quickoption_icon_voice_selector</item>
        <item name="quickoption_icon_scan_selector">@drawable/day_quickoption_icon_scan_selector</item>
        <item name="quickoption_icon_note_selector">@drawable/day_quickoption_icon_note_selector</item>
    </style>


<!-- 夜间模式 -->
    <style name="AppBaseTheme_Night" parent="@style/Theme.AppCompat">
        <item name="colorPrimary">@color/night_colorPrimary</item>
        <item name="colorPrimaryDark">@color/night_colorPrimaryDark</item>
        <item name="android:textColorLink">@color/night_colorPrimary</item>
        <item name="android:windowBackground">@color/night_windows_bg</item>
        <item name="textColor">@color/night_textColor</item>
        <item name="infoTextColor">@color/night_infoTextColor</item>
        <item name="layout_bg_normal">@color/night_layout_bg_normal</item>
        <item name="lineColor">@color/night_line_color</item>
        <item name="windows_bg">@color/night_windows_bg</item>
        <item name="sliding_block_bg">@drawable/night_image_sliding_block</item>
        <item name="layout_item_bg">@drawable/night_list_item_background</item>
        <item name="android:editTextColor">@color/night_edit_color</item>
        <item name="android:textColorHint">@color/night_edit_hit_color</item>
        <item name="comment_reply_container_bg">@drawable/night_comment_reply_container_bg</item>
        <item name="message_bg_left">@drawable/night_chat_from_bg_normal</item>
        <item name="message_bg_right">@drawable/chat_to_bg_pressed</item>
        <item name="quickoption_icon_text_selector">@drawable/night_quickoption_icon_text_selector</item>
        <item name="quickoption_icon_album_selector">@drawable/night_quickoption_icon_album_selector</item>
        <item name="quickoption_icon_photo_selector">@drawable/night_quickoption_icon_photo_selector</item>
        <item name="quickoption_icon_voice_selector">@drawable/night_quickoption_icon_voice_selector</item>
        <item name="quickoption_icon_scan_selector">@drawable/night_quickoption_icon_scan_selector</item>
        <item name="quickoption_icon_note_selector">@drawable/night_quickoption_icon_note_selector</item>
    </style>

点击切换日间/夜间模式的按钮是在侧滑菜单NavigationDrawerFragment中,

在NavigationDrawerFragment中通过ButterKnife注解找见menu_item_theme:

@InjectView(R.id.menu_item_theme)
    View mMenu_item_theme;


下面是点击事件:

case R.id.menu_item_theme:
        //点击完成模式的切换
     switchTheme();
     break;

来看关键的switchTheme()方法:

  private void switchTheme() {
        if (AppContext.getNightModeSwitch()) {
            AppContext.setNightModeSwitch(false);
        } else {
            AppContext.setNightModeSwitch(true);
        }

        if (AppContext.getNightModeSwitch()) {
            getActivity().setTheme(R.style.AppBaseTheme_Night);
        } else {
            getActivity().setTheme(R.style.AppBaseTheme_Light);
        }

        getActivity().recreate();
    }

注意这里会调用Activity的recreate()方法。 也正因为这个原因,所以可以发现切换日间/夜间模式的时候会短暂的黑屏一下,体验效果并不好。

注意新版的开源中国app已经没有日间/夜间模式切换功能了,上面的文章参考了 开源中国源码学习(五)——切换皮肤(日间模式和夜间模式)。 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值