public class AddPopWindow extends PopupWindow{ private View conentView; private int w; private Context context; public AddPopWindow(final Activity context){ LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); conentView = inflater.inflate(R.layout.title_right_add, null); this.context=context; int h = context.getWindowManager().getDefaultDisplay().getHeight(); w = context.getWindowManager().getDefaultDisplay().getWidth(); // 设置SelectPicPopupWindow的View this.setContentView(conentView); // 设置SelectPicPopupWindow弹出窗体的宽 this.setWidth(w /3); // 设置SelectPicPopupWindow弹出窗体的高 this.setHeight(LayoutParams.WRAP_CONTENT); // 设置SelectPicPopupWindow弹出窗体可点击 this.setFocusable(true); this.setOutsideTouchable(true); // 刷新状态 this.update(); // 实例化一个ColorDrawable颜色为半透明 ColorDrawable dw = new ColorDrawable(0000000000); // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ,设置其他控件变化等操作 this.setBackgroundDrawable(dw); // mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog); // 设置SelectPicPopupWindow弹出窗体动画效果 this.setAnimationStyle(R.style.AnimationPreview); } public TextView getAddBaby(){ return (TextView) conentView.findViewById(R.id.pop_add_baby); } public TextView getChangeBaby(){ return (TextView) conentView.findViewById(R.id.pop_change_baby); } public TextView getDeleteBaby(){ return (TextView) conentView.findViewById(R.id.pop_delete_baby); } /** * 显示popupWindow * * @param parent */ public void showPopupWindow(View parent) {//parent是右上角的一个view if (!this.isShowing()) { // 以下拉方式显示popupwindow //this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 5); int[] locations = new int[2]; parent.getLocationOnScreen(locations);//获取view的坐标位置//显示在屏幕的2/3位置,减去自己的宽度(可以自己适当调位置,我这是3/4),在view下方加自己的高度 this.showAtLocation(parent, Gravity.NO_GRAVITY,w*2/3-parent.getMeasuredWidth()*3/4, locations[1] + parent.getMeasuredHeight()*3/4); //this.showAsDropDown(parent,w/3+ UtilTools.dip2px(context,200), locations[1] + parent.getMeasuredHeight()); } else { this.dismiss(); } } }
指定位置弹出window
最新推荐文章于 2020-12-03 20:12:00 发布