工作过程中记录一下,以免掉头就忘。
private void initPopupWindow() {
View contentView = LayoutInflater.from(getContext()).inflate(R.layout.popupwindow_list_money, null);
mPopupWindow = new PopupWindow(contentView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.setFocusable(true);
mPopupWindow.setTouchInterceptor(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//此处监听setOutsideTouchable(true)和内容上的点击事件
return false;
}
});
float density = getResources().getDisplayMetrics().density;
mPopupWindow.setWidth((int) (90*density));
mPopupWindow.setHeight((int) (230*density));
mPopupWindow.setAnimationStyle(android.R.style.Animation_Toast);
mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
private void showPopupWindow() {
final int[] locations=new int[2];
mBinding.tvMoney.getLocationOnScreen(locations);
mPopupWindow.showAtLocation(mBinding.rlMoney,Gravity.TOP,0,locations[1]-mPopupWindow.getHeight());//在指定控件正上方显示
}
本文记录了在工作中如何自定义PopupWindow,包括初始化设置、尺寸调整、触摸监听以及展示位置的处理,旨在避免遗忘关键步骤。
2091

被折叠的 条评论
为什么被折叠?



