Android 快速实现夜间模式

1.让应用继承DayNight主题

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
</resources>

要和清单文件中的name保持一致

2.新建夜间模式资源文件夹:

在res目录下新建values-night文件夹,然后在此目录下新建colors.xml文件在夜间模式下的应用的资源。当然也可以根据需要新建drawable-night,layout-night等后缀为-night的夜间资源文件夹。如下:

values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<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"?>
<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>

3.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>

4.MainActivity.java

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

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();
            }
        });
    }
}

 

可以在BaseActivity中写入固定的 ,然后其他Activity去继承

public class BaseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
      
        /**
         * MODE_NIGHT_NO: 使用亮色(light)主题,不使用夜间模式
         * MODE_NIGHT_YES:使用暗色(dark)主题,使用夜间模式
         * MODE_NIGHT_AUTO:根据当前时间自动切换 亮色(light)/暗色(dark)主题
         * MODE_NIGHT_FOLLOW_SYSTEM(默认选项):设置为跟随系统,通常为MODE_NIGHT_NO
         */
      
        getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
    }
}

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值