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() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
if (arg2 == 11) {
popupWindow.dismiss();
}
}
});
menuGrid.setOnKeyListener(new OnKeyListener() {// 焦點到了gridview上,所以需要監聽此處的鍵盤事件。否則會出現不響應鍵盤事件的情況
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();
}