夜间模式

本文介绍如何在Android应用中实现日间与夜间模式的切换,包括修改styles.xml以支持主题变化,创建并配置values-night文件夹以实现颜色适配,以及通过编写Java代码来检测并切换当前模式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 在res目录下的Values目录下的styles.xml里面添加:

<resources>

    <!--一定要将Light改DayNight-->
    <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>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
    <!--日夜间模式切换的动画-->
    <style name="WindowAnimationFadeInOut">
        <item name="android:windowExitAnimation">@anim/fade_out</item>
        <item name="android:windowEnterAnimation">@anim/fade_in</item>
    </style>



</resources>

2、res目录下新建values-night文件夹,文件夹名字必须是values-night,再吧上边colors.xml复制到新创的values-night文件夹下,并在values-night文件夹下的colors.xml里面粘贴一下颜色

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#2b2c35</color>
    <color name="colorPrimaryDark">#15161d</color>
    <color name="colorAccent">#FF4081</color>
</resources>
res下创建anim文件夹,创建2个xml添加动画效果
第一个xml:fade_in
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:duration="1000"
        android:fromAlpha="0"
        android:interpolator="@android:anim/decelerate_interpolator"
        android:toAlpha="1.0" />
</set>

第二个xml:fade_out
 
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:duration="1000"
        android:fromAlpha="1.0"
        android:interpolator="@android:anim/decelerate_interpolator"
        android:toAlpha="0" />
</set>
3、在主线程面写,或者在Button监听里面写;代码意思是:检测当前是夜间模式还是白天模式

package com.example.aaaaaaa;

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.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tv = findViewById(R.id.tv);

        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //日夜模式
                changeDayNitht();

            }
        });

    }



    //日夜模式
    private void changeDayNitht() {//更换日夜
        int mode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
        if(mode == Configuration.UI_MODE_NIGHT_YES) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
            getWindow().setWindowAnimations(R.style.WindowAnimationFadeInOut);
        } else if(mode == Configuration.UI_MODE_NIGHT_NO) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            getWindow().setWindowAnimations(R.style.WindowAnimationFadeInOut);
        }
        recreate();
    }

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值