砖垛之PopupWindow

编程入门 行业动态 更新时间:2024-10-04 09:35:16

砖垛之<a href=https://www.elefans.com/category/jswz/34/1760522.html style=PopupWindow"/>

砖垛之PopupWindow

一、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//遮住状态栏fitPopupWindowOverStatusBar(window, true);} else {window = new CustomPopupWindow(v, WindowManager.LayoutParams.MATCH_PARENT, hight);//创建window}window.setFocusable(true); // 设置popWindow弹出窗体可点击,这句话必须添加,并且是truewindow.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() {@Overridepublic 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);public static void fitPopupWindowOverStatusBar(PopupWindow mPopupWindow, boolean needFullScreen) {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {try {Field mLayoutInScreen = PopupWindow.class.getDeclaredField("mLayoutInScreen");mLayoutInScreen.setAccessible(needFullScreen);mLayoutInScreen.set(mPopupWindow, needFullScreen);} catch (NoSuchFieldException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}}}
}

三、自定义解决在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);}@Overridepublic 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);}}

更多推荐

砖垛之PopupWindow

本文发布于:2024-02-28 06:12:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1768305.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PopupWindow

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!