Android系列之夜间模式和日间模式的功能实现

来源:互联网 发布:海淘宝宝衣服 编辑:程序博客网 时间:2024/06/03 15:41

功能的实现,首先你得把项目中的一些配置文件的信息给改一下

一、把styles中的文件信息改为:

原来的:

<resources>    <!-- Base application theme. -->    <style name="AppTheme" parent="Theme.AppCompat.Light">        <!-- Customize your theme here. -->        <item name="colorPrimary">@color/colorPrimary</item>        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>        <item name="colorAccent">@color/colorAccent</item>    </style></resources>
改为:

<resources>    <!-- Base application theme. -->    <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>

二、之后在res的下面创一个values-night文件夹,cope其中values中的colors.xml文件,修改其中的颜色成以下模样

<?xml version="1.0" encoding="utf-8"?><resources>    <color name="colorPrimary">#2196F3</color>    <color name="colorPrimaryDark">#1976D2</color>    <color name="colorAccent">#CDDC39</color></resources>
这样子之后配置信息就差不多可以了,因为我们要用到系统默认的夜间模式,所以需要改这些信息

================================================================================

实例代码:

一、xml布局文件

<?xml version="1.0" encoding="utf-8"?><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" tools:context="com.zking.laci.android13_test.MainActivity">    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="日间"        android:id="@+id/ri"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="夜间"        android:id="@+id/ye"        /></LinearLayout>

二、java代码:

package com.zking.laci.android13_test;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.support.v7.app.AppCompatDelegate;import android.view.View;public class MainActivity extends AppCompatActivity implements  View.OnClickListener{    {        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        findViewById(R.id.ri).setOnClickListener(this);        findViewById(R.id.ye).setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()){            case R.id.ri:{                getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);                recreate();                break;            }            case R.id.ye:{                getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);                recreate();            }        }    }}

这样子日间模式和夜间模式就可以出来了,是不是觉得很简单呢~~~~时间不早了,大家晚安啦,有什么不解或者发现哪里有问题的,欢迎评论哦!










原创粉丝点击