Android夜间模式显示实现方式

时间:2022-03-24 10:21:11
在oncreat方法中 初始化的到窗体服务 mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);@Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();if (nightMode) {offNightMode();记得销毁System.out.println("removeViewImmediate");}}public void offNightMode(){mWindowManager.removeViewImmediate(layout);}public void night() {WindowManager.LayoutParams lp = new WindowManager.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.TYPE_APPLICATION,WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,PixelFormat.TRANSLUCENT);lp.gravity = Gravity.BOTTOM;// 可以自定义显示的位置// lp.y = 10;// 距离底部的距离是10像素 如果是 top 就是距离top是10像素layout = new LinearLayout(this);layout.setBackgroundColor(0x99000000);mWindowManager.addView(layout, lp);}

相关文章