这几天,每当微信有新的通知,不论你在任何一个页面,别的引用的页面也好,都会在屏幕顶部出现一个悬浮窗,来显示收到消息,而不是很low的通知栏,这是怎么做的呢?
其实就是现实一个系统级的窗口:
核心代码:
public NotificationFloatingWindow(Context context, String operateMenu,
String message) {
this.inflater = LayoutInflater.from(context);
<strong> popView = (LinearLayout) inflater.inflate(R.layout.notification_layout,
null); //自己用xml定义窗口的样式</strong>
popView.setOnClickListener(this);
<strong>wm = (WindowManager) context.getApplicationContext().getSystemService(
"window"); //</strong>
this.context = context;
init(operateMenu);
}
public NotificationFloatingWindow(Context context,
NotificationReference reference) {
this.inflater = LayoutInflater.from(context);
popView = (LinearLayout) inflater.inflate(R.layout.notification_layout,
null);
popView.setOnClickListener(this);
wm = (WindowManager) context.getApplicationContext().getSystemService(
"window");
this.context = context;
init(reference);
}
每次显示的时候调用:
WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();
<strong>wmParams.type = 2002; // type是关键,这里的2002表示系统级窗口,你也可以试试2003。</strong>
wmParams.format = 1;
wmParams.gravity = Gravity.TOP;
wmParams.flags = 40;
wmParams.width = WindowManager.LayoutParams.MATCH_PARENT;
wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
<strong> wmParams.windowAnimations = R.style.notification_anim_style; //还可以自定义弹出,消失动画</strong>
wm.addView(popView, wmParams);