当前位置: 代码迷 >> Android >> Android-Popupwindow和Dialog作弹出窗口
  详细解决方案

Android-Popupwindow和Dialog作弹出窗口

热度:15   发布时间:2016-04-28 05:49:37.0
Android-Popupwindow和Dialog做弹出窗口

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

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

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent">    <ImageView        android:id="@+id/img1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:src="@drawable/img" />        <ImageView        android:id="@+id/img2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_toRightOf="@id/img1"        android:layout_marginLeft="50dp"        android:layout_alignTop="@id/img1"        android:src="@drawable/img" /></RelativeLayout>
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
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content">    <RelativeLayout         android:id="@+id/menu_layout"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true">        	    <TextView	        android:id="@+id/menu_camera"	        android:layout_width="100dp"	        android:layout_height="50dp"	        android:gravity="center"	        android:clickable="true"	        android:text="相机拍照"	        android:textSize="16sp"	        android:textColor="@android:color/black"	        android:background="@android:color/white"/>	    	    <View	        android:id="@+id/menu_sep"	        android:layout_width="100dp"	        android:layout_height="1dp"	        android:background="@android:color/black"	        android:layout_below="@id/menu_camera"/>	    	    <TextView	        android:id="@+id/menu_album"	        android:layout_width="100dp"	        android:layout_height="50dp"	        android:layout_below="@id/menu_sep"	        android:gravity="center"	        android:clickable="true"	        android:text="选取图片"	        android:textSize="16sp"	        android:textColor="@android:color/black"	        android:background="@android:color/white"/>	</RelativeLayout>	    <ImageView             android:id="@+id/arrow_up"            android:layout_width="wrap_content"        	android:layout_height="wrap_content"        	android:layout_below="@id/menu_layout"        	android:layout_marginTop="0dp"        	android:layout_centerHorizontal="true"        	android:src="@drawable/arrow_up"/>    </RelativeLayout>
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));去掉。





  相关解决方案