码迷,mamicode.com
首页 > 其他好文 > 详细

简单的日间模式夜间模式

时间:2016-07-01 08:56:42      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

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;
}
}

简单的日间模式夜间模式

标签:

原文地址:http://www.cnblogs.com/bwandroid/p/5631775.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!