展会信息港展会大全

Android-Popupwindow和Dialog做弹出窗口
来源:互联网   发布日期:2015-10-13 07:30:58   浏览:2130次  

导读:有一个需求是:在一个图片按钮上点击,在按钮的上方弹出一个弹框,根据弹框的内容页面做不同的显示。这个其实没什么难的,主要是要控制好弹框的显示位置,让弹框显示在图片的正上方的中间。一开始是用的P...

有一个需求是:在一个图片按钮上点击,在按钮的上方弹出一个弹框,根据弹框的内容页面做不同的显示。这个其实没什么难的,主要是要控制好弹框的显示位置,让弹框显示在图片的正上方的中间。

一开始是用的Popupwindow,但是Popupwindow不能给弹窗之外的页面加一个半透明的蒙层,当然可以在页面上加一个专门的作为蒙层的View,但是很显然,这么做会代码变得很恶心,于是又换成了Dialog,因为Dialog弹出的时候会自动加一个蒙层的,但是这个时候,弹框显示位置的Y坐标不对了,后来一顿查,原来Dialog默认是带有title的,只要把title去掉就可以了,看代码:

activity_main.xml:

MainActivity.java:public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

final ImageView img1 = (ImageView)this.findViewById(R.id.img1);

img1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

usePopup(img1);

}

});

final ImageView img2 = (ImageView)this.findViewById(R.id.img2);

img2.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

useDialog(img2);

}

});

}

}menu.xml

MainActivity.java:

private void usePopup(final ImageView anchor){

//参考: http://www.cnblogs.com/sw926/p/3230659.html

LayoutInflater mInflater = LayoutInflater.from(this);

ViewGroup rootView = (ViewGroup)mInflater.inflate(R.layout.menu, null);

rootView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

final PopupWindow popup = new PopupWindow(this);

//setContentView之前一定要设置宽高,否则不显示

popup.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);

popup.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);

//去掉默认的背景

popup.setBackgroundDrawable(new ColorDrawable(android.R.color.transparent));

popup.setContentView(rootView);

//点击空白处的时候PopupWindow会消失

popup.setTouchable(true);

popup.setOutsideTouchable(true);

//如果focusable为false,在一个Activity弹出一个PopupWindow,按返回键,由于PopupWindow没有焦点,会直接退出Activity。如果focusable为true,PopupWindow弹出后,所有的触屏和物理按键都有PopupWindows处理。

popup.setFocusable(true);

//计算弹框位置

int[] xy = calcPopupXY(rootView,anchor);

//不用任何gravity,使用绝对的(x,y)坐标

popup.showAtLocation((View)anchor.getParent(),Gravity.NO_GRAVITY, xy[0], xy[1]);

}

private void useDialog(final ImageView anchor){

LayoutInflater mInflater = LayoutInflater.from(this);

ViewGroup rootView = (ViewGroup)mInflater.inflate(R.layout.menu, null);

rootView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

Dialog dialog = new Dialog(this);

WindowManager.LayoutParams params = dialog.getWindow().getAttributes();

params.width = WindowManager.LayoutParams.WRAP_CONTENT;

params.height = WindowManager.LayoutParams.WRAP_CONTENT;

//去掉默认的背景,下面两个都可以

dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.R.color.transparent));

//dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

//http://stackoverflow.com/questions/12348405/dialog-is-bigger-than-expected-when-using-relativelayout

//dialog默认都是有title的

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题,否则会影响高度计算,一定要在setContentView之前调用,终于明白有一个设置theme的构造函数的目的了

dialog.setContentView(rootView);

//计算弹框位置

int[] xy = calcPopupXY(rootView,anchor);

//gravity的默认值为Gravity.CENTER,即Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL.

//参考: http://www.cnblogs.com/angeldevil/archive/2012/03/31/2426242.html

dialog.getWindow().setGravity(Gravity.LEFT | Gravity.TOP);

params.x = xy[0];

params.y = xy[1];

dialog.show();

}

//参考:http://blog.csdn.net/johnny901114/article/details/7839512

private int[] calcPopupXY(View rootView, View anchor){

int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);

int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);

rootView.measure(w, h);

int popupWidth = rootView.getMeasuredWidth();

int popupHeight = rootView.getMeasuredHeight();

Rect anchorRect = Utils.getViewAbsoluteLocation(anchor);

int x = anchorRect.left + (anchorRect.right - anchorRect.left)/2 - popupWidth / 2;

int y = anchorRect.top - popupHeight;

return new int[]{x,y};

}

public static Rect getViewAbsoluteLocation(View view){

if(view == null){

return new Rect();

}

// 获取View相对于屏幕的坐标

int[] location = new int[2] ;

view.getLocationOnScreen(location);//这是获取相对于屏幕的绝对坐标,而view.getLocationInWindow(location); 是获取window上的相对坐标,本例中只有一个window,二者等价

// 获取View的宽高

int width = view.getMeasuredWidth();

int height = view.getMeasuredHeight();

// 获取View的Rect

Rect rect = new Rect();

rect.left = location[0];

rect.top = location[1];

rect.right = rect.left + width;

rect.bottom = rect.top + height;

return rect;

}

源码在:http://download.csdn.net/download/goldenfish1919/7291951

总结一下:

(1)Popupwindow在显示之前一定要设置宽高,Dialog无此限制。

(2)Popupwindow默认不会响应物理键盘的back,除非显示设置了popup.setFocusable(true);而在点击back的时候,Dialog会消失。

(3)Popupwindow不会给页面其他的部分添加蒙层,而Dialog会。

(4)Popupwindow没有标题,Dialog默认有标题,可以通过dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);取消标题

(5)二者显示的时候都要设置Gravity。如果不设置,Dialog默认是Gravity.CENTER。

(6)二者都有默认的背景,都可以通过setBackgroundDrawable(new ColorDrawable(android.R.color.transparent));去掉。

赞助本站

人工智能实验室

相关热词: android开发

上一篇:没有了...
AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港