一般用构建方法PopupWindow(View contentView, int width, int height, boolean focusable)
第一个参数是PopupWindow里面所要显示的View,可以用getLayoutInflater().inflate(R.layout.popupwindow, null);获得。
第二个参数是PopupWindow的宽度
第三个参数是PopupWindow的高度,可以LayoutParams.WRAP_CONTENT暂时设置,之后用代码setHeight(int height)调整
第四个参数是PopupWindow里面的控件是否能获取焦点,默认false,也可通过PopupWindow的setFocusable方法设置
构建PopupWindow对象pop后需要显示在屏幕上,通过pop.showAtLocation(findViewById(R.id.parent), Gravity.CENTER, 0, 0);方法显示在屏幕上,
其中第一个参数是PopupWindow所依托的View,R.id.parent为main布局里的一个控件id,建议用LinearLayout的id。第三四个参数是PopupWindow在屏幕上的X、Y轴位置。
pop.setTouchable(false)方法可以让PopupWindow只能显示,不能点击,点击就会dismiss。
第一个参数是PopupWindow里面所要显示的View,可以用getLayoutInflater().inflate(R.layout.popupwindow, null);获得。
第二个参数是PopupWindow的宽度
第三个参数是PopupWindow的高度,可以LayoutParams.WRAP_CONTENT暂时设置,之后用代码setHeight(int height)调整
第四个参数是PopupWindow里面的控件是否能获取焦点,默认false,也可通过PopupWindow的setFocusable方法设置
构建PopupWindow对象pop后需要显示在屏幕上,通过pop.showAtLocation(findViewById(R.id.parent), Gravity.CENTER, 0, 0);方法显示在屏幕上,
其中第一个参数是PopupWindow所依托的View,R.id.parent为main布局里的一个控件id,建议用LinearLayout的id。第三四个参数是PopupWindow在屏幕上的X、Y轴位置。
这样虽然PopupWindow能正常显示,但是当没有写执行PopupWindow的dissmiss方法时,PopupWindow取消不了,回退键和menu键无效,只有home键有用,解决办法就是给它设背景图pop.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.screen_background_light));
当focusable设为false时必须pop.setOutsideTouchable(true),这样当点击PopupWindow之外的地方时能取消PopupWindow。也能点击popWindow之外的控件。
如果focusable为true,则点击popWindow之外popWindow会dissmiss。
pop.setTouchable(false)方法可以让PopupWindow只能显示,不能点击,点击就会dismiss。
ListPopWindow:
listPopupWindow = new ListPopupWindow(this);
ListAdapter adapter = new ArrayAdapter<String>(this,
android.R.layout.select_dialog_item, items);
listPopupWindow.setAnchorView(editview);
listPopupWindow.setAdapter(adapter);
listPopupWindow.setOnItemClickListener(this);
listPopupWindow.setModal(true);
listPopupWindow.setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
listPopupWindow.show();