`
whao189
  • 浏览: 122984 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

android 中弹出透明模态框

 
阅读更多
此文章有本人亲撰,但是下面所提到的code 并非本人所写写这篇日志的目的在于记录这个知识点防止以后忘记同时也希望能帮助可能用到这种效果的人!

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

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

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

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

第二种方式:
	LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
		ViewGroup menuView = (ViewGroup) mLayoutInflater.inflate(
				R.layout.gridview_pop, null, true);
		menuGrid = (GridView) menuView.findViewById(R.id.gridview);
		menuGrid.setAdapter(getMenuAdapter(menu_name_array, menu_image_array));
		menuGrid.requestFocus();
		menuGrid.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				if (arg2 == 11) {
					popupWindow.dismiss();
				}
			}
		});
		menuGrid.setOnKeyListener(new OnKeyListener() {// 焦点到了gridview上,所以需要监听此处的键盘事件。否则会出现不响应键盘事件的情况
					@Override
					public boolean onKey(View v, int keyCode, KeyEvent event) {
						switch (keyCode) {
						case KeyEvent.KEYCODE_MENU:
							if (popupWindow != null && popupWindow.isShowing()) {
								popupWindow.dismiss();
							}
							break;
						}
						System.out.println("menuGridfdsfdsfdfd");
						return true;
					}
				});
		popupWindow = new PopupWindow(menuView, LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT, true);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		popupWindow.setAnimationStyle(R.style.PopupAnimation);
		popupWindow.showAtLocation(findViewById(R.id.parent), Gravity.CENTER
				| Gravity.CENTER, 0, 0);
		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
。。。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics