如果你想要绑定和触发点击事件,用法如下
一、通过popup去找控件ID
PopupWindow popup;
popup.findViewById(R.id.btn).setOnClickListener(onClickListener);
实例:
ButtonBgUi bt = viewPop.findViewById(R.id.bt_price_add_baobao);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(mContext,"baobao",Toast.LENGTH_LONG).show();
}
});
//实例化创建PopupWindow()
//参数(窗体的视图,宽,高)
int height = getActivity().getWindowManager().getDefaultDisplay().getWidth();
PopupWindow popup = new PopupWindow(viewPop, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//背景颜色
//设置弹出窗口的动画样式
popup.setAnimationStyle(android.R.style.Animation_Dialog);
popup.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.alert_dark_frame));
//设置背景透明色
popup.getBackground().setAlpha(5);
//设置点击边上可以消失
popup.setOutsideTouchable(true);
//设置是否可以获取焦点
popup.setFocusable(true);
//设置是否可以触摸
popup.setTouchable(true);
//设置软件盘
popup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
//开始显示show 位置的设置
popup.showAtLocation(getView(), Gravity.NO_GRAVITY, 100, 100);
二、LayoutInflater的获取
第一种方式: 从给定的上下文中获取LayoutInflater:
LayoutInflater inflater = LayoutInflater.from(context);
第二种方式:在Activity中获取LayoutInflater:
LayoutInflater inflater = getLayoutInflater();
第三种方式:通过context.getSystemService()获取:
LayoutInflater inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);