PopupWindow

来源:互联网 发布:医用软件 编辑:程序博客网 时间:2024/05/29 14:10

PopupWindow是Android上自定义弹出窗口,使用起来很方便。与AlertDialog类似,区别在于AlertDialog不能指定显示位置,只能默认显示在屏幕最中间(当然也可以通过设置WindowManager参数来改变位置)。而PopupWindow是可以指定显示位置的,随便哪个位置都可以,更加灵活。

构造函数:

public PopupWindow(View contentView, int width, int height, boolean focusable)
当然popupwindow还有其他构造函数,但要生成一个PopupWindow最基本的三个条件是一定要设置的:View contentView,int width, int height 这三个属性,否则popupwindow将不起作用.因为popupwindow没有布局,所以必须准备一个xml文件.用来填充popupwindow

popupwindow显示

 private void showPopupWindow() {          //设置contentView          View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null);          mPopWindow = new PopupWindow(contentView,                  LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);              //设置各个控件的点击响应          TextView tv1 = (TextView)contentView.findViewById(R.id.pop_computer);          TextView tv2 = (TextView)contentView.findViewById(R.id.pop_financial);          TextView tv3 = (TextView)contentView.findViewById(R.id.pop_manage);          tv1.setOnClickListener(this);          tv2.setOnClickListener(this);          tv3.setOnClickListener(this);          //显示PopupWindow          View rootview = LayoutInflater.from(MainActivity.this).inflate(R.layout.main, null);          mPopWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);        }  
注意:

// 使其聚集
popupWindow.setFocusable(true);
// 设置允许在外点击消失
popupWindow.setOutsideTouchable(true);
// 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景
popupWindow.setBackgroundDrawable(new BitmapDrawable())


销毁popupwindow:

调用popupwindow的dismiss方法

 



 

原创粉丝点击