夜间模式 (文字不改变)
Posted 天空很大,我们很小
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了夜间模式 (文字不改变)相关的知识,希望对你有一定的参考价值。
package com.chenke.night;
import java.nio.MappedByteBuffer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity{
private WindowManager wm;
private View myView;
private Button night;
private SharedPreferences sp;
private final static String DAY = "day";
private final static String NIGHT = "night";
private int flage = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
setContentView(R.layout.activity_main);
sp = this.getSharedPreferences("skinchange", Context.MODE_PRIVATE);
night = (Button) findViewById(R.id.btn_dayAndnight);
String mode = sp.getString("skin", "");
if(mode!=null||!mode.equals("")){
if(mode.equals(NIGHT)){
night();
}else{
day();
}
}
night.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(flage%2==0){
night();
night.setText("白天模式");
night.setTextColor(Color.WHITE);
flage++;
}else{
day();
night.setText("夜间模式");
night.setTextColor(Color.BLACK);
flage++;
}
}
});
}
public void night() {
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,
LayoutParams.TYPE_APPLICATION,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity=Gravity.BOTTOM;
params.y=10;
if(myView==null){
myView=new TextView(this);
myView.setBackgroundColor(0x80000000);
}
wm.addView(myView, params);
Editor edit = sp.edit();
edit.putString("skin", NIGHT);
edit.commit();
}
public void day(){
if(myView!=null){
wm.removeView(myView);
Editor edit = sp.edit();
edit.putString("skin", DAY);
edit.commit();
}
}
}
以上是关于夜间模式 (文字不改变)的主要内容,如果未能解决你的问题,请参考以下文章
系统Android 10.0来袭:新增夜间模式,最大赢家是OLED手机用户?
安卓开发。用webView做一个简单浏览器,想实现夜间模式(反色),就是改变网页的背景色,应该怎么