日夜间模式切换第二种方式

//工程自带的values文件夹中

//colors.xml文件

<?xml version="1.0"encoding="utf-8"?>

<resources>

    <colorname="colorPrimary">#3F51B5</color>

    <colorname="colorPrimaryDark">#303F9F</color>

    <colorname="colorAccent">#FF4081</color>

 

    <colorname="main_textView">#FF0000</color>

</resources>

 

//工程自带的values文件夹中

//strings.xml文件

<resources>

        //自己的项目名

    <stringname="app_name">DayNightTwo</string>

    <stringname="change_to_content">切换成夜间模式</string>

</resources>

 

//自己创建一个文件夹  values-night

将以上两个文件粘贴过来

//colors.xml文件中

<?xml version="1.0"encoding="utf-8"?>

<resources>

    <colorname="colorPrimary">#35464e</color>

    <colorname="colorPrimaryDark">#212a2f</color>

    <colorname="colorAccent">#212a2f</color>

   

    <colorname="main_textView">#00FF00</color>

</resources>

 

//strings.xml中

<resources>

    <stringname="app_name">DayNightTwo</string>

    <stringname="change_to_content">切换成日间模式</string>

</resources>

 

//activity_main中的布局设置

<?xml version="1.0"encoding="utf-8"?>

<LinearLayoutxmlns: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"

   tools:context="com.example.android_day_night.MainActivity"

   android:gravity="center_horizontal"

    >

 

    <Button

       android:text="@string/change_to_content"

        android:id="@+id/change_theme_btn"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_marginTop="30dp"

       android:gravity="center_horizontal"

       android:onClick="onClick" />

 

    <TextView

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_marginTop="100dp"

       android:text="Hello World!"

       android:textColor="@color/main_textView" />

 

    <TextView

        android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_marginTop="20dp"

       android:text="Hello World!"

       android:textColor="@color/main_textView" />

 

    <TextView

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_marginTop="20dp"

       android:text="Hello World!"

       android:textColor="@color/main_textView" />

       

</LinearLayout>

 

//创建一个自定义的Application在清单文件中配置

public class App extends Application {

    @Override

    public voidonCreate() {

       super.onCreate();

        if(getSharedPreferences("theme",MODE_PRIVATE).getBoolean("night_theme", false)) {

           AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);

        }

    }

}

 

//MainActivity中

//按钮的点击监听事件

public void onClick(View v) {

        //切换日夜间模式

        int uiMode;

        uiMode =getResources().getConfiguration().uiMode &Configuration.UI_MODE_NIGHT_MASK;

        switch(uiMode) {

            case Configuration.UI_MODE_NIGHT_YES:

               AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

               getSharedPreferences("theme",MODE_PRIVATE).edit().putBoolean("night_theme", false).commit();

                break;

            caseConfiguration.UI_MODE_NIGHT_NO:

               AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);

               getSharedPreferences("theme",MODE_PRIVATE).edit().putBoolean("night_theme", true).commit();

                break;

        }

        recreate();

    }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值