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 ...
随机推荐
- busybox sz rz命令
之前板子和电脑之间传送文件的时候都是通过tftp网络下载.所以找了一下在文件系统中使用串口上传文件的方法. rz和sz命令使用zmoderm协议,SecureCRT也用提供这个命令的支持.由于是串口, ...
- background-image中url找不到路径,背景图像无法显示
<div id="body_left_secend_1"> <img src="~/Images/imag ...
- PHP加密解密字符串
项目中有时我们需要使用PHP将特定的信息进行加密,也就是通过加密算法生成一个加密字符串,这个加密后的字符串可以通过解密算法进行解密,便于程序对解密后的信息进行处理. 最常见的应用在用户登录以及一些AP ...
- EF Code First教程-01 创建一个简单的Code First程序
1 从nuget中搜索并添加EF 2 在app.config或web.config中添加数据库连接 <connectionStrings> <add name="conns ...
- ORA-28056:Writing audit records to Windows EventLog failed的问题
右键“我的电脑”--‘管理’--事件查看器--点击左边的日志(应用程序日志.安全日志.系统日志),右击某个项目,选择‘清除所有事件'即可删除.
- 为什么Button点击了没反应,反而其他事件反应了
- python MySQLdb中文乱码
Python操作MySQL需要安装Python-MySQL可以从网上搜索一下,和一般的Python包一样安装 安装好之后,模块名字叫做MySQLdb ,在Window和Linux环境下都可以使用,试验 ...
- Java基础之集合框架——在文件中存储地图(TryPhoneBook2)
控制台程序. import java.io.*; public class Person implements Comparable<Person>, Serializable { // ...
- devexpress13学习系列(三)PDFViewer(3)
PdfDocumentProperties Class 该类,用来显示载入的pdf文件的属性,包括: Name Description Application Indicates the appl ...
- 在windows下配置pthread
http://blog.csdn.net/qianchenglenger/article/details/16907821 简单介绍windows平台下的pthread线程库