今天开始做女神免费网站,中国建设银行北京招聘信息网站,网站建站思路,个体户做网站是怎么备案释放双眼#xff0c;带上耳机#xff0c;听听看~#xff01;一、概述1、PopupWindow与AlertDialog的区别最关键的区别是AlertDialog不能指定显示位置#xff0c;只能默认显示在屏幕最中间(当然也可以通过设置WindowManager参数来改变位置)。而PopupWindow是可以指定显示位置…释放双眼带上耳机听听看~一、概述1、PopupWindow与AlertDialog的区别最关键的区别是AlertDialog不能指定显示位置只能默认显示在屏幕最中间(当然也可以通过设置WindowManager参数来改变位置)。而PopupWindow是可以指定显示位置的随便哪个位置都可以更加灵活。2、PopupWindow的相关函数第一步最基本构造PopupWindowView contentView LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null);PopupWindwo popWnd PopupWindow (context);popWnd.setContentView(contentView);popWnd.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);popWnd.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);拓展也可以缩写成为两句话View contentView LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null);PopupWindwo popWnd new PopupWindow (contextViewViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);第二步上面的代码基本就生成了一个简单的PopUpWindow但是想要显示出来还需要以下方法显示函数主要使用下面三个//相对某个控件的位置(正左下方)无偏移showAsDropDown(View anchor)//相对某个控件的位置有偏移;xoff表示x轴的偏移正值表示向左负值表示向右yoff表示相对y轴的偏移正值是向下负值是向上showAsDropDown(View anchor, int xoff, int yoff)//相对于父控件的位置(例如正中央Gravity.CENTER下方Gravity.BOTTOM等)可以设置偏移或无偏移showAtLocation(View parent, int gravity, int x, int y)这里有两种显示方式1、显示在某个指定控件的下方showAsDropDown(View anchor)showAsDropDown(View anchor, int xoff, int yoff)2、指定父视图显示在父控件的某个位置(Gravity.TOP,Gravity.RIGHT等)showAtLocation(View parent, int gravity, int x, int y)第三步显示窗体通过showAsDropDown显示出来但是从哪里显示出来还没有定义有同学就会问难道不是在第一步布局文件显示的吗那只是显示的布局并没有说在哪里显示所以我们还是要加载主窗口在主窗口显示View rootview LayoutInflater.from(MainActivity.this).inflate(R.layout.main, null);mPopWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);完整代码Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);View contentView LayoutInflater.from(MainActivity.this).inflate(R.layout.pewm, null);mPopWindow new PopupWindow(contentView,ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);//显示PopupWindowView rootview LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_main, null);mPopWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);}