PopUpWindow实现半透明弹出框关键点:
-
布局文件 最外层设置为全屏 背景颜色为半透明
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/translucent"
android:gravity="center"
android:orientation="vertical"
>
.......
<ListView
android:id="@+id/qianghaoqi_diary_pop_list"
android:divider="@null"
android:scrollingCache="false"
android:fadingEdge="none"
android:scrollbarThumbVertical="@drawable/game_blade_qianghaoqi_listview_scrollbar"
android:layout_marginBottom="@dimen/dip5"
android:layout_marginLeft="@dimen/dip5"
android:layout_marginRight="@dimen/dip10"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:listSelector="@null"
android:paddingLeft="15.0dip"
/>
.........
</RelativeLayout>
-
new一个全屏的PopUpWindow
//必须为true,可以获取焦点
mPopWin = new PopupWindow(aPopView, LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT,true);
//mPopWin.setHeight(450);//设置PopupWindow高度
//必须设置。改变弹出窗口的背景,当然也可以设置为NULL。
mPopWin.setBackgroundDrawable(mActivity.getResources().getDrawable(R.drawable.game_blade_qianghaoqi_transparent_bg));
mPopWin.showAtLocation(aParentView,Gravity.CENTER, 0, 0);
//如果窗口已经显示过,更改此值只能在下一次显示时起作用,或者调用update()
mPopWin.update();
-
如果PopUpWindow内的布局会夺取焦点(如示例ListView),注意代码
mListView.requestFocus();
// 焦点到了listView上,所以需要监听此处的键盘事件。否则会出现不响应键盘事件的情况
mListView.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
closePopWin();
}
return true;
}
});
-
关闭PopUpWindow
private void closePopWin(){
if (mPopWin != null && mPopWin.isShowing()) {
mPopWin.dismiss();
}
}
-
PopUpWindow一般应用
mPopWin = new PopupWindow(mPopView, LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
mPopWin.setBackgroundDrawable(mActivity.getResources().getDrawable(R.drawable.game_blade_qianghaoqi_transparent_bg));
mPopWin.setOutsideTouchable(true);
mPopWin.setFocusable(true);
mPopWin.showAsDropDown(aParentView,0,-15);
mPopWin.update();