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(); } } }}
这样子日间模式和夜间模式就可以出来了,是不是觉得很简单呢~~~~时间不早了,大家晚安啦,有什么不解或者发现哪里有问题的,欢迎评论哦!
阅读全文
0 0
- Android系列之夜间模式和日间模式的功能实现
- Android 切换日间夜间模式
- 日间模式夜间模式切换小功能
- android换肤(日间模式和;夜间模式)
- 设置日间夜间模式
- 日间、夜间模式切换
- 夜间与日间模式的切换
- 日间模式转换夜间模式
- Android之夜间模式的实现
- android 画面风格切换(夜间模式/日间模式) (有小坑没解决)
- 系统自带方法设置日间和夜间模式切换
- android夜间模式的实现
- Android夜间模式的实现
- android 夜间模式的实现
- Android 夜间模式的实现
- android夜间模式的实现
- android夜间模式的实现
- 关于日间夜间模式切换的一种实用方法
- Windows server 2012配置Nginx自启动
- 工厂模式
- android员工管理系统小程序
- 二维数组的使用(new,delete,初始化,输出)
- 51Nod-1765-谷歌的恐龙
- Android系列之夜间模式和日间模式的功能实现
- Eclipse新建一个web项目没有自动生成web.xml
- 设计模式(十二)adapter
- ATC作业
- Swing 利用线程来绘制两个运动的小球
- Docker aufs存储驱动layer、diff、mnt目录的区别
- OpenCV中使用imread函数读取图片失败的原因
- Your password does not satisfy the current policy requirements centos7 mysql 修改密码
- C语言之编码注意事项