您的位置:首页 > 移动开发 > Android开发

android 6.0 应用主题切换

2016-03-08 19:00 417 查看
在support 23.2中增加了日夜间主题:

详细应用:

第一步:在application 中设置默认主题

<pre name="code" class="java">package com.xuan.theme;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatDelegate;

/**
* @author xuanyouwu
* @email xuanyouwu@163.com
* @time 2016-03-09 09:44
*/
public class BaseApplication extends Application {

public static SharedPreferences sp;
public static final String THEMID = "themId";

@Override
public void onCreate() {
super.onCreate();
sp = getSharedPreferences("sp", Context.MODE_PRIVATE);
if (sp.getInt(THEMID, 1) == 2) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}
}



第二步:夜间模式:



第三步:

设置夜间模式:

getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES);
recreate();


设置日间模式:

getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO);
recreate();


效果图:



源码:在群里:476899320
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: