package com.loaderman.daynightdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.widget.Toolbar;
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);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar); findViewById(R.id.btn_auto).setOnClickListener(this);
findViewById(R.id.btn_day).setOnClickListener(this);
findViewById(R.id.btn_night).setOnClickListener(this);
findViewById(R.id.fab).setOnClickListener(this);
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_auto: {
getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
recreate();
break;
}
case R.id.btn_day: {
getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
recreate();
break;
}
case R.id.btn_night: {
getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
recreate();
break;
}
}
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
android:fitsSystemWindows="true"
tools:context="com.yanzhenjie.daynight.MainActivity"> <android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_main"/> <android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email"/> </android.support.design.widget.CoordinatorLayout>

content_main.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"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.yanzhenjie.daynight.MainActivity"
tools:showIn="@layout/activity_main"> <Button
android:id="@+id/btn_auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自动模式" /> <Button
android:id="@+id/btn_day"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="日间模式" /> <Button
android:id="@+id/btn_night"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="夜间模式" />
</LinearLayout>

style.xml

<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> <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" /> </resources>

AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.loaderman.daynightdemo"> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>

效果图:

最新文章

  1. Entity Framework 6 Code First新特性:支持存储过程
  2. gradle.properties
  3. html5画布的旋转效果
  4. Oracle分页语句
  5. HDU 1251 统计难题
  6. JBPM流程实例(PI)Process Instance
  7. IniParse解析类
  8. 在Mapper中进行循环判断
  9. Python Socket Programming
  10. shell的wc命令统计 head tail命令详解
  11. AspNetCore.Hosting
  12. HTML5分析实战WebSockets一个简短的引论
  13. Java之路——敬JAVA初学者(作者:MoMo)
  14. TP3.2写提交的验证码验证
  15. (hdu 6024) Building Shops
  16. 【GitHub】的基本使用
  17. Java函数式编程和lambda表达式
  18. 504. Base 7
  19. CentOS 7.4 使用源码包编译安装MySQL 5.7.20
  20. Redis高速内存缓冲平台可视化监控之RedisLive配置实战

热门文章

  1. SQL Server里面如何导出包含数据的SQL脚本
  2. Codeforces Round #581 (Div. 2)A BowWow and the Timetable (思维)
  3. 【转】Java的四种代码块
  4. 【2017 北京集训 String 改编版】子串
  5. 一例tornado框架下利用python panda对数据进行crud操作
  6. JAVA笔记2-面向对象与内存解析
  7. vue启动问题(You may use special comments to disable some warnings. Use // eslint-disable-next-line to ignore the next line. Use /* eslint-disable */ to ignore all warnings in a file.)
  8. 【WinForm-无边框窗体】实现Panel移动窗体,没有边框的窗体
  9. Python 面向对象Ⅳ
  10. 《剑指offer》算法题第一天