您的位置:首页 > 运维架构

砖垛之PopupWindow

2017-08-14 14:53 309 查看
一、PopupWindow弹出后,当里面有editext时,要弹出键盘,这时键盘会遮挡pop

解决;

popupWindow.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);


注意;这两个设置的顺序不能变,并且要在设置showAtLocation之前设置

二、封装

/**
* description: pop工具类
* autour: mo
* date: 2017/7/28 0028 15:15
*/
public abstract class UtilPop {
public interface PopCallBack {
void getData(PopupWindow window, String selectId, int position);
}

/**
* @param context      上下文
* @param layoutId     布局id
* @param atLocationId 显示位置id
* @param hight        高度状态 0 自适应 1 胀满
* @param upOrDown     0从下往上弹,1 从上往下弹
*/
public UtilPop(Activity context, int layoutId, int atLocationId, int hight, int upOrDown) {
View v = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(layoutId, null);//填充视图
PopupWindow window;
if (hight == 0) {
window = new CustomPopupWindow(v, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);//创建window
} else if (hight == 1) {
window = new CustomPopupWindow(v, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);//创建window
} else {
window = new CustomPopupWindow(v, WindowManager.LayoutParams.MATCH_PARENT, hight);//创建window
}

window.setFocusable(true); // 设置popWindow弹出窗体可点击,这句话必须添加,并且是true
window.setBackgroundDrawable(new ColorDrawable(0xb000000)); // 实例化一个ColorDrawable颜色为半透明

if (upOrDown == 0) {
//解决PopupWindow含有输入框时,点击输入框,软键盘可能会挡住PopupWindow 顺序不能变
window.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
window.setAnimationStyle(R.style.mypopwindow_anim_style); // 设置popWindow的显示和消失动画
window.showAtLocation(context.findViewById(atLocationId), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); // 在底部显示

} else if (upOrDown == 1) {
window.setAnimationStyle(R.style.pop_top_anim_style); // 设置popWindow的显示和消失动画
window.showAsDropDown(context.findViewById(atLocationId)); // 在底部显示
}
// 设置背景颜色变暗
final Window bgWindow = context.getWindow();
WindowManager.LayoutParams lp = bgWindow.getAttributes();
//        lp.alpha = 0.7f;
lp.alpha = 0.95f;
context.getWindow().setAttributes(lp);
window.setOnDismissListener(new PopupWindow.OnDismissListener() {

@Override
public void onDismiss() {
WindowManager.LayoutParams lp = bgWindow.getAttributes();
lp.alpha = 1f;
bgWindow.setAttributes(lp);
}
});

doWhat(v, window, bgWindow);

}

protected abstract void doWhat(View v, PopupWindow window, Window bgWindow);
}


三、自定义解决在7.0及其他版本中无法正常在设置的位置显示

public class CustomPopupWindow extends PopupWindow {

public CustomPopupWindow(View contentView, int width, int height) {
this(contentView, width, height, false);
}
public CustomPopupWindow(View contentView, int width, int height, boolean focusable) {
super(contentView, width, height, focusable);
}

@Override
public void showAsDropDown(View anchor) {
if (Build.VERSION.SDK_INT == 24) {
Rect rect = new Rect();
anchor.getGlobalVisibleRect(rect);
int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
setHeight(h);
}
super.showAsDropDown(anchor);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PopupWindo 键盘 遮挡