android 中弹出透明模态框

需求 就是通过 点击一个button 弹出来 类似于Dialog这样的效果 
这里我们可以通过两种方式实现 
第一种:就是通过dialog 
第二种:通过PopupWindow 

第一种如下重要的伪代码如下: 
我们需要通过 dialog 弹出来一个 listview 
Java代码   收藏代码
  1. private void openDialog() {  
  2. View menuView = View.inflate(this, R.layout.gridview_menu, null);  
  3.         // 创建AlertDialog  
  4. final AlertDialog menuDialog = new AlertDialog.Builder(this).create();  
  5. menuDialog.setView(menuView);  
  6. menuGrid = (GridView) menuView.findViewById(R.id.gridview);  
  7. menuGrid.setAdapter(getMenuAdapter(menu_name_array,menu_image_array));  
  8. menuGrid.setOnItemClickListener(new OnItemClickListener() {  
  9. @Override  
  10. public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {  
  11.     if (arg2 == 11) {  
  12.           menuDialog.cancel();  
  13.     }  
  14.     }  
  15. });  
  16.     menuDialog.show();  
  17. }  

首先拿到 你要显示的listview 组件 
然后我们通过设置dialog的view 来显示 显示出来之后 至于 你要在listview 中得item 中 实现什么功能那就要自己 添加了 

dialog这一种方式 只是 通过 一个自定义的view 来填充 dialog 本身包含的view(我个人理解是这样的。。) 

第二种方式: 
Java代码   收藏代码
  1. LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);  
  2.     ViewGroup menuView = (ViewGroup) mLayoutInflater.inflate(  
  3.             R.layout.gridview_pop, nulltrue);  
  4.     menuGrid = (GridView) menuView.findViewById(R.id.gridview);  
  5.     menuGrid.setAdapter(getMenuAdapter(menu_name_array, menu_image_array));  
  6.     menuGrid.requestFocus();  
  7.     menuGrid.setOnItemClickListener(new OnItemClickListener() {  
  8.         @Override  
  9.         public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
  10.                 long arg3) {  
  11.             if (arg2 == 11) {  
  12.                 popupWindow.dismiss();  
  13.             }  
  14.         }  
  15.     });  
  16.     menuGrid.setOnKeyListener(new OnKeyListener() {// 焦点到了gridview上,所以需要监听此处的键盘事件。否则会出现不响应键盘事件的情况  
  17.                 @Override  
  18.                 public boolean onKey(View v, int keyCode, KeyEvent event) {  
  19.                     switch (keyCode) {  
  20.                     case KeyEvent.KEYCODE_MENU:  
  21.                         if (popupWindow != null && popupWindow.isShowing()) {  
  22.                             popupWindow.dismiss();  
  23.                         }  
  24.                         break;  
  25.                     }  
  26.                     System.out.println("menuGridfdsfdsfdfd");  
  27.                     return true;  
  28.                 }  
  29.             });  
  30.     popupWindow = new PopupWindow(menuView, LayoutParams.FILL_PARENT,  
  31.             LayoutParams.FILL_PARENT, true);  
  32.     popupWindow.setBackgroundDrawable(new BitmapDrawable());  
  33.     popupWindow.setAnimationStyle(R.style.PopupAnimation);  
  34.     popupWindow.showAtLocation(findViewById(R.id.parent), Gravity.CENTER  
  35.             | Gravity.CENTER, 00);  
  36.     popupWindow.update();  


个人理解 就是 LayoutInflater (布局填充者) 获取到我们事先定义好的布局界面 

PopupWindow(menuView, LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT, true); 

定义弹出来的 组件填充全屏  

popupWindow.setBackgroundDrawable(new BitmapDrawable()); 

这个地方一定要设置 而且一定要设置在 

popupWindow.showAtLocation(findViewById(R.id.parent), Gravity.CENTER 
| Gravity.CENTER, 0, 0); 

之前 
我猜想是这样的。。如果popupWindow 没有 画好背景的话他是不会弹出来的,(他必须把需要的东西准备好才会弹出来) .. 
至于findViewById(R.id.parent),  这个东西就是你要弹出在那个view之上的那个view Id 
。。。 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值