夜间模式实现简单步骤,可有立竿见影之效果

这是一个简单的夜间模式实现方式,
步骤简单。可供初学者使用
首先下载https://download.youkuaiyun.com/download/qq_41762084/10678900这是一个deme类型的jar;
使用前请安步骤操作
第一步创建
values文件下的
atts.xml
设置颜色切换状态的名字

<?xml version="1.0" encoding="utf-8"?>
<resources>
                <attr name="zzbackground" format="color|reference"/>
                <attr name="zzbackgroundDrawable" format="reference"/>
                <attr name="zztextColor" format="color"/>
                <attr name="zzItemBackground" format="color"/>
                <attr name="hui_yijian" format="color"/>
                <attr name="hui_item" format="color"/>
                <attr name="colorAccent" format="color"/>
                </resources>

colors.xml
配置需要用到的颜色

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#EB3F3B</color>
    <color name="red">#DA2C15</color>
    <color name="red1">#DD403B</color>
    <color name="hui">#FF331515</color>
    <color name="hui1">#5B5B5B</color>
    <color name="hui2">#B5B4B4</color>
    <color name="white">#FFFFFF</color>
    <color name="black">#000000</color>
    <color name="ceng">#DD403B</color>

    <color name="hui_yijian01">#746464</color>

    <color name="ceng1">#FF9000</color>
    <color name="black1">#3A3A3A</color>
    <color name="colorPrimaryNight">#263238</color>
    <color name="colorPrimaryDarkNight">#1E282D</color>
    <color name="colorAccentNight">#73432A</color>

    <color name="dayBackground">#F2F4F7</color>
    <color name="dayTextColor">#000</color>
    <color name="dayItemBackground">#fff</color>

    <color name="nightItemBackground">#37474F</color>
    <color name="nightBackground">#263238</color>
    <color name="nightTextColor">#fff</color>
    //白黑
    <color name="set_title_white">#ef3919</color>
    <color name="set_title_black">#792929</color>

</resources>


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>
        <item name="windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>
        <item name="windowNoTitle">true</item>
    </style>
    <style name="DayTheme" parent="AppTheme">
        <item name="zzbackground">@color/dayBackground</item>//
        <item name="zzbackgroundDrawable">@drawable/ic_launcher_foreground</item>
        <item name="zztextColor">@color/dayTextColor</item>
        <item name="zzItemBackground">@color/dayItemBackground</item>
        <item name="hui_item">@color/hui</item>
        <item name="hui_yijian">@color/hui_yijian01</item>

    </style>
    <style name="NightTheme" parent="AppTheme">
        <item name="hui_yijian">@color/dayItemBackground</item>
        <item name="hui_item">@color/dayItemBackground</item>
        <item name="zzbackground">@color/nightBackground</item>
        <item name="zzbackgroundDrawable">@color/nightBackground</item>
        <item name="zztextColor">@color/nightTextColor</item>
        <item name="zzItemBackground">@color/nightItemBackground</item>

        <item name="colorPrimary">@color/colorPrimaryNight</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDarkNight</item>
        <item name="colorAccent">@color/colorAccentNight</item>
    </style>
    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

做完以上操作后
接下来需要在xml布局里设置布局的背景
记住
每一个可以看见的布局都应该设置背景,
具体操作如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_marginTop="@dimen/x9"
    android:background="?attr/zzbackground"
    app:backgroundAttr="zzbackground"
   >
           <TextView
            android:layout_centerVertical="true"
            android:id="@+id/item_grxx"
            android:layout_width="match_parent"
            android:layout_height="@dimen/x20"
            android:layout_margin="@dimen/x9"
            android:gravity="center_vertical"
            android:text="个人信息"
            android:background="?attr/zzItemBackground"
            app:backgroundAttr="zzItemBackground"
            android:textColor="?attr/hui_item"
            app:textColorAttr="hui_item"
            tools:ignore="MissingPrefix" />
    </LinearLayout>

在需要操作的Activity里的
onCreate方法下面一行
添加如下代码:

 ChangeModeController.getInstance().init(this, R.attr.class).setTheme(this, R.style.DayTheme, R.style.NightTheme);
        

设置监听按钮

 if (is) {
                //第二步 设置切换
                Toast.makeText(context, "夜间模式开", Toast.LENGTH_SHORT).show();
                ChangeModeController.changeNight((Activity) context, R.style.NightTheme);
                ChangeModeController.getAttrTypedValue((Activity) context, R.attr.zztextColor);
                bo(is);
            } else {
                bo(is);
                ChangeModeController.changeDay((Activity) context, R.style.DayTheme);
                ChangeModeController.getAttrTypedValue((Activity) context, R.attr.zztextColor);
                Toast.makeText(context, "夜间模式关", Toast.LENGTH_SHORT).show();

            }

在onDstroy里设置

@Override
    protected void onDestroy() {
        super.onDestroy();
        //第三步   在onDestroy调用
        ChangeModeController.onDestory();
    }

以上都是操作步骤,没有讲实现原理,但是可以达到立竿见影的效果
如有不懂的地方,可以留言交流,里面不足,望大家多多批评指正

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值