Android實現夜間模式

現在很是多App都有夜間模式,特別是閱讀類的App。夜間模式現在已是閱讀類App的標配了,其實,日間模式與夜間模式就是給App定義並應用兩套不一樣顏色的主題,用戶可以本身主動或者手動的開啓,今天用Android自帶的support包來實現夜間模式。html

由於Support Library在23.2.0的版本號中才加入了Theme.AppCompat.DayNight主題,因此依賴的版本號必須是高於23.2.0的。並且,這個特性支持的最低SDK版本號爲14,因此。需要兼容Android 4.0的設備,是不能使用這個特性的。在API Level 14下面的設備會默認使用亮色主題。java

只是現在4.0下面的設備應該比較少了吧,畢竟微信的minSdkVersion都設置爲14了。android

加入依賴微信


準備資源app

讓應用繼承DayNight主題ide

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>
新建夜間模式資源目錄:

res目錄下新建values-night目錄,而後在此目錄下新建colors.xml文件在夜間模式下的應用的資源。固然也可以依據需要新建drawable-night,layout-night等後綴爲-night的夜間資源目錄。例如如下:
post


內容例如如下:ui

values/colors.xmlspa

<?

xml version="1.0" encoding="utf-8"?code

> <!-- day values colors.xml --> <resources> <color name="colorPrimary">#009688</color> <color name="colorPrimaryDark">#00796B</color> <color name="colorAccent">#009688</color> <color name="textColorPrimary">#616161</color> <color name="viewBackground">@android:color/white</color> <color name="colorDayNightChange">@android:color/holo_orange_dark</color> </resources>

values/strings.xml
<resources>
    <string name="app_name">DayNight</string>
    <string name="day_night_label">日間模式</string>
</resources>
values-night/colors.xml
<?

xml version="1.0" encoding="utf-8"?> <!-- night values colors.xml --> <resources> <color name="colorPrimary">#35464e</color> <color name="colorPrimaryDark">#212a2f</color> <color name="colorAccent">#212a2f</color> <color name="textColorPrimary">#616161</color> <color name="viewBackground">#212a2f</color> <color name="colorDayNightChange">@android:color/holo_blue_dark</color> </resources>

values-night/strings.xml
<resources>
    <string name="app_name">DayNight</string>
    <string name="day_night_label">夜間模式</string>
</resources>

使Activity繼承自AppCompatActivity


在Application中設置初始主題


動態切換


代碼邏輯實現例如如下:

acitivity_main.xml

<?

xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="10dp" android:layout_marginRight="10dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="@string/day_night_label" android:textSize="20sp" android:textColor="@color/colorDayNightChange" /> <Button android:id="@+id/day_night_change" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginLeft="5dp" android:text="日夜間模式切換" android:textSize="20sp" android:textColor="@color/colorDayNightChange"/> </LinearLayout>

MainActivity.java

package com.jackie.daynight;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button mDayNightChange;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mDayNightChange = (Button) findViewById(R.id.day_night_change);

        mDayNightChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int mode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
                if (mode == Configuration.UI_MODE_NIGHT_YES) {
                    getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                } else if (mode == Configuration.UI_MODE_NIGHT_NO) {
                    getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                }

                recreate();
            }
        });
    }
}

MyApplication.java

package com.jackie.daynight;

import android.app.Application;
import android.support.v7.app.AppCompatDelegate;

/**
 * Created by Jackie on 2017/3/6.
 * Application
 */

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        /**
         * 默認設置一直使用夜間模式
         *
         * 這裏AppCompatDelegate.setDefaultNightMode()方法可以接受的參數值有4個:
         * MODE_NIGHT_NO. Always use the day (light) theme(一直應用日間(light)主題).
         * MODE_NIGHT_YES. Always use the night (dark) theme(一直使用夜間(dark)主題).
         * MODE_NIGHT_AUTO. Changes between day/night based on the time of day(依據當前時間在day/night主題間切換).
         * MODE_NIGHT_FOLLOW_SYSTEM(默認選項). This setting follows the system’s setting, which is essentially MODE_NIGHT_NO(尾隨系統,一般爲MODE_NIGHT_NO).
         */
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    }
}
效果例如如下:

相關文章
相關標籤/搜索