简单的日间模式夜间模式
Posted 白日梦游
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单的日间模式夜间模式相关的知识,希望对你有一定的参考价值。
package com.example.topnews.topnews;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;
/**
* Created by Mr雷 on 2016/6/23.
*/
public class DayOrNight extends FragmentActivity {
public WindowManager mWindowManger;
// 设置夜间模式
public SharedPreferences skinSp;
public final static String DAY = "day";
public final static String NIGHT = "night";
public boolean isDay;
public View myView;
public ImageView imageView_night;
public TextView text_night;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mWindowManger=(WindowManager) getSystemService(WINDOW_SERVICE);
init();
}
// 日间夜间模式切换
public void init() {
skinSp = getSharedPreferences("skinchange", MODE_PRIVATE);
String mode = skinSp.getString("skin","");
if (mode != null || !mode.equals("")) {
if (mode.equals(NIGHT)) {
night();
isDay = false;
} else {
day();
isDay = true;
}
}
}
public void night() {
@SuppressWarnings("deprecation")
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.FILL_PARENT,
LayoutParams.TYPE_APPLICATION,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.CENTER;
// params.y = 10;
if (myView == null) {
myView = new View(this);
myView.setBackgroundColor(0x80000000);
}
mWindowManger.addView(myView, params);
Editor edit = skinSp.edit();
edit.putString("skin", NIGHT);
edit.commit();
}
public void day() {
if (myView != null) {
mWindowManger.removeView(myView);
Editor edit = skinSp.edit();
edit.putString("skin", DAY);
edit.commit();
}
}
@Override
public void onDestroy() {
super.onDestroy();
String mode = skinSp.getString("skin", "");
if (mode.equals(NIGHT)) {
removeSkin();
}
}
public void removeSkin() {
if (myView != null) {
mWindowManger.removeView(myView);
}
}
}
-------------------------------------------实现------------------------------------------------
String mode = skinSp.getString("skin","");
if (mode != null || !mode.equals("")) {
if (mode.equals(NIGHT)) {
imgMode.setImageResource(R.drawable.mode_night);
tvMode.setText("夜间模式");
day();
isDay = true;
} else {
imgMode.setImageResource(R.drawable.mode_day);
tvMode.setText("日间模式");
night();
isDay = false;
}
}
以上是关于简单的日间模式夜间模式的主要内容,如果未能解决你的问题,请参考以下文章