1、什么是popWindow?
popWindow就是对话框的一种方式!此文讲解的android中对话框的一种使用方式,它叫popWindow。
2、popWindow的特性
Android的对话框有两种:PopupWindow和AlertDialog。它们的不同点在于:
AlertDialog的位置固定,而PopupWindow的位置可以随意。
AlertDialog是非阻塞线程的,而PopupWindow是阻塞线程的。
PopupWindow的位置按照有无偏移分,可以分为偏移和无偏移两种;按照参照物的不同,可以分为相对于某个控件(Anchor锚)和相对于父控件。具体如下
showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移
showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移
showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移
自己的注意点:
首先要先
// 定义一个窗口private PopupWindow menu;
// 为这个窗口定义一个容器
private View popView;
private void initPopWindow() {
// 声明layoutInflater
LayoutInflater layoutInflater = (LayoutInflater) this
.getSystemService(LAYOUT_INFLATER_SERVICE);
// 确定popView容器
popView = layoutInflater.inflate(R.layout.menu, null);
// 将PopupWindow放到容器中去
menu = new PopupWindow(popView, LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
// 设置popupWindow焦点不仅在自己身上。点屏幕别的地方可以隐藏掉popUpWindow
menu.setOutsideTouchable(true);
// 使焦点放在popUpWindow上
// menu.setFocusable(true)
// 给其设置背景,这样配合上面给的outSide才可以实现点击别的地方使其消失的效果
menu.setBackgroundDrawable(new ColorDrawable(0xb0000000));
//设置进入动画
menu.setAnimationStyle(R.style.AnimBottom);
// 声明pop里面的Button控件
Button menuButton = (Button) popView.findViewById(R.id.btn_menu);
//声明取消的Button控件
Button quxiaoButton = (Button) popView.findViewById(R.id.btn_cancel);
quxiaoButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
menu.dismiss();
}
});
// 设置点击事件
menuButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 设置菜单栏隐藏
menu.dismiss();
// 开启反馈界面
agent.startFeedbackActivity();
}
});
}
上面是初始化一个PopupWindow的具体一些细节,一般判断显示就是PopupWindow.isshowing()来进行相关的操作