android开发之popupwindow的使用及细节
- 初始化
- 显示
- 隐藏
- 使用细节
popupwindow的初始化
public void initDanmuPop() {
// 一个自定义的布局,作为显示的内容
View contentView = LayoutInflater.from(this).inflate(R.layout.chat_danmu, null);
popupWindow = new PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT, DensityUtil.dip2px(this,48f), true);
popupWindow.setTouchable(true); //控制是否可点击
popupWindow.setFocusable(true); //控制是否聚焦,能否响应点击事件的关键
//是否可点击外部取消
//为true时,必须要设置背景才会生效;为false时,必须不设置背景才会生效
popupWindow.setOutsideTouchable(true);
popupWindow.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
popupWindow.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
//软键盘不会挡着popupwindow
popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
// 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
popupWindow.setBackgroundDrawable(new BitmapDrawable());
//内部控件初始化
ivShortcut = (ImageView) contentView.findViewById(R.id.iv_shortcut);
ivSend = (ImageView) contentView.findViewById(R.id.iv_send);
ivCancel = (ImageView) contentView.findViewById(R.id.iv_cancel);
etDanmu = (EditText) contentView.findViewById(R.id.et_danmu);
tvEdit = (TextView) contentView.findViewById(R.id.tv_edit);
slvShortcut = (SliderListView) contentView.findViewById(R.id.slv_shortcuts);
llShortcut = (LinearLayout) contentView.findViewById(R.id.ll_shortcut);
//消失监听
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
}
});
}
popupwindow的显示
1、相对某个控件的位置(正左下方),无偏移
popupWindow.showAsDropDown(view);
2、相对某个控件的位置(正左下方),有偏移
popupWindow.showAsDropDown(mTv, width, 0);//设置x轴偏移量:注意单位为px
3、 相对于父控件的位置,无偏移~参数1为父容器~参数2为相对父容器相对类型~参数3、4为偏移量
popupWindow.showAtLocation(parentView, Gravity.CENTER, 0, 0);
4、相对于父控件的位置,有偏移~参数1为父容器~参数2为相对父容器相对类型~参数34为偏移量
popupWindow.showAtLocation(parentView, Gravity.BOTTOM, 20, 20);
popupwindow的隐藏
if (mPopupWindow != null && mPopupWindow.isShowing()) {
mPopupWindow.dismiss();
}
细节部分
1、在屏幕下方不被系统键盘遮挡的处理
//获取焦点
popupWindow.setFocusable(true);
//设置不被系统键盘遮挡
popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED); popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
//弹出键盘时会被顶起
popupWindow.showAtLocation(parentView, Gravity.BOTTOM | Gravity.CENTER_VERTICAL, 0, 0);
2、内部有EditText自动获取焦点的情况
在EditText父容器内加上下面两行代码
android:focusable=”true”
android:focusableInTouchMode=”true”
目的是让父容器得到焦点,从而使Edittext失去焦点
3、update方法的使用
在使用弹窗的过程中,经常会遇到需要改变弹窗显示大小的情况,可以使用弹窗自带的update(int width,int heigth)方法即可达到这一效果,而不需关闭弹窗在打开。
4、点击外部区域,弹窗不消失,但是点击事件会向下面的activity传递
在工具类中写上下面这个方法:
UIUtils.setPopupWindowTouchModal(popupWindow, false);
public static void setPopupWindowTouchModal(PopupWindow popupWindow,
boolean touchModal) {
if (null == popupWindow) {
return;
}
Method method;
try {
//运用的反射原理
method = PopupWindow.class.getDeclaredMethod("setTouchModal",
boolean.class);
method.setAccessible(true);
method.invoke(popupWindow, touchModal);
}
catch (Exception e) {
e.printStackTrace();
}
}
再在程序中调用:
UIUtils.setPopupWindowTouchModal(popupWindow, false);
这样即可实现在弹窗不消失的情况下该popupWindow外部的事件传递给下面的Activity了。