本文主要是介绍Android Popwindow 使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
代码如下:
private PopupWindow mPopupWindow;tv.setOnClickListener(v -> {if (mPopupWindow != null && mPopupWindow.isShowing()) {return;}mPopupWindow = new PopupWindow();mPopupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);mPopupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));View rootView = inflater.inflate(R.layout.fragment_nio, null);mPopupWindow.setContentView(rootView);mPopupWindow.setOutsideTouchable(true);mPopupWindow.setSplitTouchEnabled(true);//pop弹出后,不希望外面的控件收到点击事件 那么就设置focusable 为 true//这样就不会出现点击弹出控件 就会出现先popwindow 先消失 再显示mPopupWindow.setFocusable(true);
// popupWindow.setWindowLayoutType(WindowManager.LayoutParams.TYPE_APPLICATION);mPopupWindow.setBackgroundDrawable(getContext().getDrawable(R.drawable.ripple_background));PopupWindowCompat.showAsDropDown(mPopupWindow,tv,-200,20, Gravity.LEFT);mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {@Overridepublic void onDismiss() {Window window = getActivity().getWindow();WindowManager.LayoutParams attributes = window.getAttributes();attributes.alpha = 1;window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);window.setAttributes(attributes);}});Window window = getActivity().getWindow();WindowManager.LayoutParams attributes = window.getAttributes();attributes.alpha = 0.3f;window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);window.setAttributes(attributes);});
showAsDropDown
* @param anchor the view on which to pin the popup window* @param xoff A horizontal offset from the anchor in pixels* @param yoff A vertical offset from the anchor in pixels* @param gravity Alignment of the popup relative to the anchor** @see #dismiss()*/public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) {
anchor 表示你想关于在哪个View 附近显示PopWindow,
xoff 表示 popWindow 左上角距离anchor 左下角 的x 距离,右边xoff 为正,左边为负
yoff 表示popwindow 左上角距离anchor 左下角 的 y 距离,下边为正,上面为负
gravity 设置为 Gravity.START (好像Gravity.END 不好用,还是通一设置为START,然后计算坐标吧。折腾了半天)
这篇关于Android Popwindow 使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!