PopupWindow相比于Dialog框,PopupWindow是阻塞性的,Dialog框并不是阻塞性的。
PopupWindow定位布局比较方便,可以居于某个布局的下面。位子比较好控制。但是Dialog需要靠像素来调整。而且PopupWindow显隐比较方便,不用你进行判断。如果已经显示,则直接进行隐藏。
下面贴出PopupWindow的代码:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.title_select_layout, null);
//从dimen文件中获取固定尺寸
float width=MyApp.getInstance().getResources().getDimension(R.dimen.select_dialog);
PopupWindow window = new PopupWindow(view,(int)width, WindowManager.LayoutParams.WRAP_CONTENT);
//设置背景透明度
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 0; //0.0-1.0
getWindow()