android简单的夜间模式
现在android项目values下打

attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="bookimage" format="reference|color" />
<attr name="tvcolor" format="reference|color" />
</resources>
style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 默认风格 -->
<style name="BrowserThemeDefault" parent="@android:style/Theme.Black.NoTitleBar">
<item name="bookimage">@android:color/white</item>
<item name="tvcolor">@android:color/darker_gray</item>
</style> <!-- 夜间模式 -->
<style name="BrowserThemeNight" parent="@android:style/Theme.Black.NoTitleBar">
<item name="bookimage">@android:color/transparent</item>
<item name="tvcolor">@android:color/white</item>
</style>
</resources>

layout下activity_main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
       //界面颜色改变
      android:background="?bookimage"
     >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" 
        //字体颜色改变
        android:textColor="?tvcolor"/>
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        //监听方法
        android:onClick="btonclick"
        android:text="日/夜间模式切换" />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="116dp"
        android:onClick="btonclick2"
        android:text="跳转其他页面" />
</RelativeLayout>

MainActivity

package com.example.zdndemo; import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View; public class MainActivity extends Activity {
private static boolean blFlag = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); SharedPreferences preferences = getSharedPreferences("default_night",
MODE_PRIVATE);
blFlag = preferences.getBoolean("default_night",true);
if (blFlag) {
this.setTheme(R.style.BrowserThemeDefault);
}
else {
this.setTheme(R.style.BrowserThemeNight);
}
//上面的代码必须要放在setContentView之上 setContentView(R.layout.activity_main);
} public void btonclick(View view) {
SharedPreferences preferences = getSharedPreferences("default_night",MODE_PRIVATE);
Editor editor = preferences.edit();
if (blFlag) {
this.setTheme(R.style.BrowserThemeNight);
blFlag =false;
editor.putBoolean("default_night",false);
} else {
this.setTheme(R.style.BrowserThemeDefault);
blFlag = true;
editor.putBoolean("default_night",true); }
// 提交修改
editor.commit();
this.setContentView(R.layout.activity_main);
//不行的话在跳下本页面
} public void btonclick2(View view) {
Intent intent = new Intent();
intent.setClass(this, breakactivity.class);
startActivity(intent);
}
}

android简单的夜间模式的更多相关文章
- Android白天/夜间模式Day/Night Mode标准原生SDK实现
		
 Android白天/夜间模式Day/Night Mode标准原生SDK实现 章节A:Android实现白天/夜间模式主要控制器在于UiModeManager,UiModeManager是Andr ...
 - Android 利用an框架快速实现夜间模式的两种套路
		
作者:Bgwan链接:https://zhuanlan.zhihu.com/p/22520818来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 网上看到过大多实现夜间模 ...
 - 【android】夜间模式简单实现
		
完整代码,请参考我的博客园客户端,git地址:http://git.oschina.net/yso/CNBlogs 关于阅读类的app,有个夜间模式真是太重要了. 那么有两种方式可以实现夜间模式 1: ...
 - Android 之夜间模式(多主题)的实现
		
引言 夜间模式其实属于多主题切换的一种,不过是最麻烦的一种.因为在夜间模式下不仅要切换主色调,次要色调等等,还要覆盖一些特殊的颜色,因为在夜间模式下总不能什么都是黑的把,那不得丑死-.-,所以当你夜间 ...
 - Android 夜间模式changeskin小结
		
@author vivian8725118 @CSDN http://blog.csdn.net/vivian8725118 @简书 http://www.jianshu.com/p/832e9776 ...
 - Android夜间模式的几种实现
		
一.直接修改widget颜色,这种方式实现起来最简单,但需要每个控件都去修改,太过复杂.例如: /** * 相应交互,修改控件颜色 * @param view */public void onMeth ...
 - Android实现夜间模式小结
		
随着APP实现的功能越来越丰富, 看小说看视频上网等等, 如今不少人花在手机平板等移动终端上的时间越来越长了. 但手机和平板的屏幕并不像Kindle那类电纸书的水墨屏那么耐看, 因为自发光的屏幕特性, ...
 - android夜间模式实现
		
一.概述 android夜间模式实现分为两大类 重启activity的实现 不重启activity的实现 二.正文 1.重启activity实现夜间模式[在界面文件中的实现部分] 1.1在attrs. ...
 - Android主题切换—夜间/白天模式探究
		
现在市面上众多阅读类App都提供了两种主题:白天or夜间. 上述两幅图片,正是两款App的夜间模式效果,所以,依据这个功能,来看看切换主题到底是怎么实现的(当然现在github有好多PluginThe ...
 
随机推荐
- pickle 数据对象的序列化和反序列化
			
python的pickle模块实现了基本的数据序列和反序列化.通过pickle模块的序列化操作我们能够将程序中运行的对象信息保存到文件中去,永久存储:通过pickle模块的反序列化操作,我们能够从文件 ...
 - C# serialport
			
最近使用C#写串口的程序,实现串口助手的功能,参考文档记录于此. 参考文档 http://blog.csdn.net/geekwangminli/article/details/7851673 htt ...
 - Java私有构造函数不能阻止继承
			
下面是一个调用已经私有化的单列的函数的列子. 这里用了静态内部类,关键就是静态内部类可以访问外部类的私有构造函数. 这种算是变种继承吧.前提是可以在原来的单列类里添加代码. class Single ...
 - AutoMappeer自动映射
			
AutoMapper是用来解决对象之间映射转换的类库.对于我们开发人员来说,写对象之间互相转换的代码是一件极其浪费生命的事情,AutoMapper能够帮助我们节省不少时间.
 - Android大图片裁剪终极解决方案(上:原理分析)
			
转载声明:Ryan的博客文章欢迎您的转载,但在转载的同时,请注明文章的来源出处,不胜感激! :-) http://my.oschina.net/ryanhoo/blog/86842 约几个月前,我正 ...
 - 红帽Linux  配置VNC桌面远程工具
			
1.先确认VNC是否安装 默认情况下,Red Hat Enterprise Linux安装程序会将VNC服务安装在系统上. 使用rpm命令检查是否安装了vnc,如果安装了就显示软件名称: [root@ ...
 - osgEarth编译的一些问题
			
这两天借着osg培训的机会捯饬了下64位osgearth的编译.遇到了一些问题: 首先我没有编译osg,用的提供的osg3.2.1编译好的64位包. 编译osgearth先后编译了2个版本,先是2.7 ...
 - [SharpMap]二叉树索引
			
读取shp文件建立二叉树索引的基本思路分析: /// <summary> /// Generates a spatial index for a specified shape file. ...
 - Vue.2.0.5-模板语法
			
Vue.js 使用了基于 HTML 的模版语法,允许开发者声明式地将 DOM 绑定至底层 Vue 实例的数据.所有 Vue.js 的模板都是合法的 HTML ,所以能被遵循规范的浏览器和 HTML 解 ...
 - iOS 黑屏
			
1. 首先看操作系统是否有很多Bug 9.0.2 的系统不稳定. http://tech.163.com/15/1020/05/B6BL6PML000915BD.html