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 ...
随机推荐
- Estimating Project Costs
The Wideman Comparative Glossary of Common Project Management Terms describes estimating cost as, &q ...
- 四个很好用的Sql Server 日期函数:DateDiff、DatePart、DateAdd、DateName
我以前查一段时间范围内的数据都是在程序里计算好日期再掉查询语句,现在我用下面的函数.SQL SERVER没有查一季度数据的函数. DateDiff函数: 描述 返回两个日期之间的时间间隔. 语法 Da ...
- windows7 密码保护 共享文件
windows7 密码保护 共享文件 2台windows7之间设置文件共享,本想使用ftp,但是配置指定用户连接,配置权限比较繁琐. 所以就想到使用window7的文件共享,并设置密码,共享整个硬盘的 ...
- Class.getResourceAsStream() VS. ClassLoader.getResourceAsStream()
For Class.getResourceAsStream(String name), if the name parameter doesn't start with a "/" ...
- Oracle ORA-12519: TNS:no appropriate service handler found 解决
有时候连得上数据库,有时候又连不上. 可能是数据库上当前的连接数目已经超过了它能够处理的最大值. select count(*) from v$process --当前的连接数select value ...
- 3、DOM操作
一.认识DOM 文档对象模型DOM(Document Object Model)定义访问和处理HTML文档的标准方法.DOM 将HTML文档呈现为带有元素.属性和文本的树结构(节点树). 先来看看下面 ...
- 做IT不能一辈子只靠技术生存
在中国你千万不要以为学习技术就可以换来稳定的生活和高的薪水待遇,你千万更不要认为哪些从事市场开发,跑腿的人,没有前途. 不知你是不是知道,咱们中国有相当大的一部分软件公司,他们的软件开发团队都小的可怜 ...
- Alloc and release
https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractic ...
- AngularJS form表单验证(非常全面)
构建一个ng表单 novalidate=”novalidate” 2.form中不能有action属性.提交交由ng-submit处理 3.每个input一定要有ng-model,最好有一个name方 ...
- 改变对update的做法
以前都是 先根据id或者其他条件查出来 再根据查出来的结果 进行修改 再update提交 这里可以改所有的字段 现在是做法 是直接new 一个 Do或者Vo 把要改变的值 先填充进去 然后再去 ...