本文实例讲述了Android弹出窗口实现方法。分享给大家供大家参考,具体如下:
直接上代码:
/*** 弹窗--新手指引* @param cxt * @param id 资源编号* @create_time 2011-7-27 下午05:12:49*/public static void displayWindow(Context cxt, int id) {final TextView imgTV = new TextView(cxt.getApplicationContext());imgTV.setBackgroundDrawable(cxt.getResources().getDrawable(id));//设置背景final WindowManager wm = (WindowManager) cxt.getApplicationContext().getSystemService("window");WindowManager.LayoutParams wmParams = new WindowManager.LayoutParams();wmParams.type = 2002;wmParams.format = 1;wmParams.flags = 40;wmParams.width = LayoutParams.FILL_PARENT;wmParams.height = LayoutParams.FILL_PARENT;wm.addView(imgTV, wmParams);imgTV.setOnClickListener(new Button.OnClickListener() {@Overridepublic void onClick(View v) {wm.removeView(imgTV);//点击,将该窗口消失掉}});}
别忘了在AndroidManifest.xml中添加权限:
复制代码 代码如下:<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
android.permission.SYSTEM_ALERT_WINDOW
允许一个程序打开窗口使用 TYPE_SYSTEM_ALERT,显示在其他所有程序的顶层(Allows an application to open windows using the type TYPE_SYSTEM_ALERT, shown on top of all other applications. )
这个FIRST_SYSTEM_WINDOW的值就是2000。2003和2002的区别就在于2003类型的View比2002类型的还要top,能显示在系统下拉状态栏之上!
希望本文所述对大家Android程序设计有所帮助。