关闭

夜间模式实现方式

标签: 夜间模式修改themeuimode
259人阅读 评论(0) 收藏 举报
分类:

1.修改theme:

public class BaseActivity extends AppCompatActivity {

    private boolean isNightMode;
    private NightModeReceiver mNightModeReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isNightMode = AppConfig.isNightMode;
        setNightMode();
        super.onCreate(savedInstanceState);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(AppConfig.NIGHT_MODE);
        mNightModeReceiver = new NightModeReceiver();
        LocalBroadcastManager.getInstance(this).registerReceiver(mNightModeReceiver, intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mNightModeReceiver) {
            LocalBroadcastManager.getInstance(this).unregisterReceiver(mNightModeReceiver);
        }
    }

    protected void changeTheme(int styleId,int layoutResID){
        setTheme(styleId);
        setContentView(layoutResID);
    }

    protected void setNightMode(boolean isNightMode,int layoutResID){
        if (isNightMode){
            changeTheme(R.style.NightTheme,layoutResID);         
        }else {
            changeTheme(R.style.DefaultTheme, layoutResID);
        }
    }

    private void setNightMode(){
        if(isNightMode){
            setTheme(R.style.NightTheme);
        }else {
            setTheme(R.style.DefaultTheme);
        }
    }

    protected void reset(){

    }

    private class NightModeReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(AppConfig.NIGHT_MODE)){
                reset();
            }
        }
    }

}

2.修改uimode:

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        Log.d(TAG, "---onCreate()---");
    }

    private void initView() {
        findViewById(R.id.btn_night).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppConfig.isNightMode = !AppConfig.isNightMode;
                updateNightMode(AppConfig.isNightMode);
                recreate();
            }
        });
    }

    private void updateNightMode(boolean isNightMode) {
        DisplayMetrics dm = getResources().getDisplayMetrics();
        Configuration config = getResources().getConfiguration();
        config.uiMode &= ~Configuration.UI_MODE_NIGHT_MASK;
        config.uiMode |=
                isNightMode ? Configuration.UI_MODE_NIGHT_YES : Configuration.UI_MODE_NIGHT_NO;
        getResources().updateConfiguration(config, dm);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "---onDestroy()---");
    }

    @Deprecated
    private void setNightMode() {
        //开启行车模式,可进入夜间效果
        AppConfig.isNightMode = !AppConfig.isNightMode;
        UiModeManager uiModeManager = (UiModeManager) getSystemService(Context.UI_MODE_SERVICE);
        if (AppConfig.isNightMode) {
            uiModeManager.enableCarMode(0);
            uiModeManager.setNightMode(UiModeManager.MODE_NIGHT_YES);
        } else {
            uiModeManager.enableCarMode(0);
            uiModeManager.setNightMode(UiModeManager.MODE_NIGHT_NO);
        }
    }

}

下载地址:
http://download.csdn.net/detail/piratecode/9615249

https://github.com/piratecode/NightModelProject

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:13121次
    • 积分:535
    • 等级:
    • 排名:千里之外
    • 原创:43篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类