android夜间模式的实现
项目中要用到夜间模式,今天我就来总结,我是怎么实现的吧
实现原理:我们把当前的主题存放在sp中,每当activity启动之前调用setTheme方法设置相应的主题,setTheme方法一定要在super.onCreate()方法之前执行。
(1)在values文件夹下, 分别写一套夜间模式下的布局(theme_dark.xml)
和白天模式下的布局(theme_light.xml),
theme_dark.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme_Night" >
<item name="username_double">@color/night_gray_1</item>
<item name="gray_1_double">@color/gray_1_night</item>
<item name="gray_3_double">@color/gray_3_night</item>
<item name="source_bg">@drawable/source_bg_night</item>
</style>
</resources>
theme_light.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme_Light" >
<item name="username_double">@color/light_gray_1</item>
<item name="gray_1_double">@color/light_gray_1</item>
<item name="gray_3_double">@color/gray_3</item>
<item name="source_bg">@drawable/source_bg_light</item>
</style>
</resources>
(2) 在values文件下新建一个theme_attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <attr name="username_double" format="color" /> <attr name="gray_1_double" format="color" /> <attr name="gray_3_double" format="color" /> <attr name="source_bg" format="reference" /> </resources>
前面3个是颜色属性,后面是一个drawable类型,分别和 theme_light.xml和theme_night一一对应。当然还可以是状态选择器,shape图形资源。
(3) 在activity中我们要在super.onCreate()方法之前调用setTheme()方法,由于项目中的每个activity都要调用该方法,所以我把这个方法放在BaseActivity中实现。
public class BaseActivity extends Activity {
protected int skin;
public Context mContext;
SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
setThemeMode(getSkinTypeValue());
super.onCreate(savedInstanceState);
Global.mContext = this;
this.mContext = this;
sp = getSharedPreferences("AppSkinType", Context.MODE_PRIVATE);
}
protected void setThemeMode(SkinType skinType) {
switch (skinType) {
case Light:
setTheme(R.style.AppTheme_Light);
break;
case Night:
setTheme(R.style.AppTheme_Night);
break;
default:
setTheme(R.style.AppTheme_Light);
break;
}
}
protected SkinType getSkinTypeValue() {
if (sp == null) {
sp = getSharedPreferences("AppSkinType", Context.MODE_PRIVATE);
}
int i = sp.getInt("AppSkinType", 0);
switch (i) {
case 0:
return SkinType.Light;
case 1:
return SkinType.Night;
default:
break;
}
return SkinType.Light;
}
protected void saveSkinValue(int skin) {
if (sp == null) {
sp = getSharedPreferences("AppSkinType", Context.MODE_PRIVATE);
}
Editor editor = sp.edit();
editor.putInt("AppSkinTypeValue", skin);
editor.commit();
}
}
(4)下面是我写的一个列子,布局很简单,就是一个textView和chebox,checkBox选中的时候便设置成夜间模式,没有选中便为白天模式
代码如下:
package com.example.activity;
import com.example.night_mode.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class MainActivity extends BaseActivity {
private CheckBox cbSetting;
private String Tag = "mainActivity";
private Button btnForward;
private View llNight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
llNight = findViewById(R.id.ll_night);
btnForward = (Button) findViewById(R.id.forward);
cbSetting = (CheckBox) findViewById(R.id.cb_setting);
cbSetting.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (cbSetting.isChecked()) {
// 设置夜间模式
saveSkinValue(1);
llNight.setBackgroundDrawable(getResources().getDrawable(R.drawable.source_bg_night));
} else {
// 设置白天模式
saveSkinValue(0);
llNight.setBackgroundDrawable(getResources().getDrawable(R.drawable.source_bg_light));
}
}
});
btnForward.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,
SecondActivity.class);
startActivity(intent);
}
});
}
}
代码已贴出
相关推荐
包含三个Android工程示例代码: 1. 使用降低屏幕亮度的方式实现夜间模式 2. 使用替换theme的方式实现夜间模式 3. webView实现白天/夜间模式阅读
Android夜间模式实现,通过在window上加一层半透明的View,解决网上坑点,亲自在项目中尝试
Android夜间模式实现,通过在window上加一层半透明的View.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
Android原生方式实现程序夜间模式,相应文档地址: http://blog.csdn.net/qq_23383563/article/details/52493320
ThemeDemo日夜间模式切换,页面切换的时候附带动画特效, 关于多主题实现的,大牛这里的做法是继承AppCompatActivity,置换了AppCompatDelegate中AppCompatViewInflater中的createView 方法.实现了对 xml 控件的控制. ...
android夜间模式切换,颜色渐变效果实现 博客地址:http://blog.csdn.net/tyzlmjj/article/details/49255019
夜间模式实现 所谓的夜间模式,就是能够根据不同的设定,呈现不同风格的界面给用户,而且晚上看着不伤眼睛,实现方式也就是所谓的换肤(主题切换)。对于夜间模式的实现网上流传了很多种方式。也反编译了几个新闻类...
实现了日夜模式的切换.(不重启 Acitivity ) 解决了因为快速点击 View 导致的多次响应点击事件. 内部实现了 Android 5.0 的CircularReveal效果. 优点: 布局中直接使用 Android 默认的控件就可以.在解析以后会根据...
通过css注入的方式实现android webview的夜间模式。
这是个方便切换夜间模式的库,利用官方夜间模式,同时不用重启Activity
Android改Theme实现夜间模式, 说明地址:http://blog.csdn.net/u012572538/article/details/38726549
特别是阅读类的App,夜间模式现在已经是阅读类App的标配了,事实上,日间模式与夜间模式就是给App定义并应用两套不同颜色的主题,用户可以自动或者手动的开启,今天用Android自带的support包来实现夜间模式。...
Android夜间模式,通过Theme实现(attrs.xml styles.xml Activity.setTheme()) 效果展示: 如何添加 Gradle添加: 1.在Project的build.gradle中添加仓库地址 allprojects { repositories { ... maven { url ...
使用要求:官方包support appcompat 23.2.0 或以上版本activity 需要继承自 AppCompatActivity应用按照官方的夜间模式实现使用:1.在appication中初始化public class AppApplication extends Application { @...
android夜间模式功能的实现,类似换肤功能,多种模式
夜间模式的实现
由于Android的设置中并没有夜间模式的选项,对于喜欢睡前玩手机的用户,只能简单的调节手机屏幕亮度来改善体验。目前越来越多的应用开始把夜间模式加到自家应用中,没准不久google也会把这项功能添加到Android系统中...
Android白天夜晚模式切换实现,用BroadCast Receiver实现,具体内容在我博客中