WindowManager.LayoutParams:
int TYPE_SYSTEM_ALERT Window type: system window, such as low power alert.(系统窗体,例如低电量警告提示框)
int TYPE_SYSTEM_OVERLAY Window type: system overlay windows, which need to be displayed on top of everything else.(系统覆盖窗体,哪个需要显示在最前的)
TYPE_SYSTEM_ALERT跟TYPE_SYSTEM_OVERLAY的区别
system_alert窗口可以获得焦点,响应操作
system_overlay窗口显示的时候焦点在后面的Activity上,仍旧可以操作后面的Activity
清单文件manifest中需要相应的permission
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />
以下两种方式显示出来的窗口都不会激发当前Activity的onPause()事件。
1. Seivece 创建窗体
1 private void showSystemDialog() { 2 /* create ui dialog */ 3 View v = View.inflate(mContext, R.layout.serveice_dialog, null); 4 AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); 5 alertDialog.setView(v); 6 dialog = alertDialog.create(); 7 dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); 8 //dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY); 9 dialog.show(); 10 **** 注意dialog 在执行show方法之后,才能调整dialog的大小以及更新数据 **** 11 /* set size & pos */ 12 WindowManager.LayoutParams lp = dialog.getWindow().getAttributes(); 13 WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); 14 Display display = wm.getDefaultDisplay(); 15 if (display.getHeight() > display.getWidth()) { 16 //lp.height = (int) (display.getHeight() * 0.5); 17 lp.width = (int) (display.getWidth() * 1.0); 18 } else { 19 //lp.height = (int) (display.getHeight() * 0.75); 20 lp.width = (int) (display.getWidth() * 0.5); 21 } 22 d.getWindow().setAttributes(lp); 23 24 /* update ui data */ 25 lv = (ListView) d.getWindow().findViewById(R.id.listview); 26 SimpleAdapter adapter = new SimpleAdapter(mContext, getListData(), R.layout.list_item, 27 new String[]{"item_text", "item_img"}, 28 new int[]{R.id.item_text, R.id.item_img}); 29 lv.setAdapter(adapter); 30 31 /* set listener */ 32 lv.setOnItemClickListener(new OnItemClickListener() { 33 public void onItemClick(AdapterView<?> parent, View view, int pos, 34 long id) { 35 d.dismiss(); 36 } 37 }); 38 39 }
2. 直接使用WindowManager.addView()在后台直接显示窗口
1 LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 2 mDesktopLayout = inflater.inflate(R.layout.volume_panel, null); 3 // 取得系统窗体 4 mWindowManager = (WindowManager) getApplicationContext().getSystemService(WINDOW_SERVICE); 5 // 窗体的布局样式 6 mLayoutParams = new WindowManager.LayoutParams(); 7 // 设置窗体显示类型――TYPE_SYSTEM_ALERT(系统提示) 8 mLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; 9 // 设置窗体焦点及触摸: 10 // FLAG_NOT_FOCUSABLE(不能获得按键输入焦点) 11 mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; 12 // 设置显示的模式 13 mLayoutParams.format = PixelFormat.RGBA_8888; 14 // 设置对齐的方法 15 mLayoutParams.gravity = Gravity.TOP | Gravity.LEFT; 16 // 设置窗体宽度和高度 17 mLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT; 18 mLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT; 19 // 设置窗体显示的位置,否则在屏幕中心显示 20 mLayoutParams.x = 50; 21 mLayoutParams.y = 50; 22 mWindowManager.addView(mDesktopLayout, mLayoutParams);
时间: 2024-10-13 01:04:22