PopupWindow的使用和分析 弹出框的一种

PopupWindow使用

       PopupWindow这个类用来实现一个弹出框,可以使用任意布局的View作为其内容,这个弹出框是悬浮在当前activity之上的。


自己的例子:

/**
	 * 创建PopupWindow
	 */
	private PopupWindow popupWindow;
	private LinearLayout popupWindow_view;
	protected void initPopuptWindow() {
		// TODO Auto-generated method stub
		// 获取自定义布局文件activity_popupwindow_left.xml的视图
		//	popupWindow_view = (LinearLayout) getLayoutInflater().inflate(R.layout.welcome, null,false);
		popupWindow_view = new LinearLayout(context);

		popupWindow_view.setBackgroundResource(R.color.blackbantouming);
		//	popupWindow_view.setBackgroundColor(R.color.red);

		// 创建PopupWindow实例,200,LayoutParams.MATCH_PARENT分别是宽度和高度LayoutParams.MATCH_PARENT

		popupWindow = new PopupWindow(popupWindow_view,LayoutParams.FILL_PARENT , LayoutParams.FILL_PARENT, 

				true);


		// 这里是位置显示方式,在屏幕的左侧
		popupWindow.setFocusable(true);
		popupWindow.setOutsideTouchable(true);
		// 设置动画效果
		//	popupWindow.setAnimationStyle(R.style.AnimationFade);
		// 点击其他地方消失
		popupWindow_view.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if (popupWindow != null && popupWindow.isShowing()) {
					popupWindow.dismiss();
					popupWindow = null;
				}
				return false;
			}
		});
	}


/***
	 * 获取PopupWindow实例
	 */
	private void getPopupWindow() {
		if (null != popupWindow) {
			popupWindow.dismiss();
			return;
		} else {
			initPopuptWindow();
		}
	}

使用时:

/**
	 * boolean sor  是否是排序(排序要放到下方)
	 * LinearLayout sumLayout 存放的控件
		final List<Datas> list	数据源
		boolean box	下方需要不
		boolean with   是否跟随子控件的宽度并居中
		final FeelForeverService feel
	 * */
	public void OneLayoutList(boolean sor,View v , final List<Datas> list, boolean box,boolean with, final FeelForeverService feel){
		getPopupWindow(); 
		//这是指定在哪个控件之下  
		if(sor){
			int[] location = new int[2];  
			v.getLocationOnScreen(location);  
			popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], location[1]-popupWindow.getHeight());  
		}else {

			popupWindow.showAsDropDown(v);

		}




		popupWindow_view.removeAllViews();
		LinearLayout layoutTitle=new LinearLayout(context);
		layoutTitle.setOrientation(LinearLayout.HORIZONTAL);

		LayoutInflater inflaterTitle = (LayoutInflater) context.getSystemService

				(Context.LAYOUT_INFLATER_SERVICE); 
		inflaterTitle.inflate(R.layout.service_onelist, layoutTitle);

		ListView list1 = (ListView) layoutTitle.findViewById(R.id.service_list_one);
		LinearLayout service_box_001= (LinearLayout) layoutTitle.findViewById(R.id.service_box_001);

		if(box) service_box_001.setVisibility(View.VISIBLE); else service_box_001.setVisibility(View.GONE);



		Button box_but_OK = (Button) layoutTitle.findViewById(R.id.box_but_OK_02);
		final EditText box_min = (EditText) layoutTitle.findViewById(R.id.box_min_02);
		final EditText box_max = (EditText) layoutTitle.findViewById(R.id.box_max_02);
		box_but_OK.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				feel.getOncliekOne("",box_min.getText().toString().trim() +"-"+ 
						box_max.getText().toString().trim(),
						(box_min.getText().toString().trim().equals("")?"0":box_min.getText

								().toString().trim()) +
								"-"+ (box_max.getText().toString().trim().equals

										("")?"9999999":box_max.getText().toString().trim()));
				popupWindow.dismiss();
			}
		});

		RelativeLayout onelistBox =(RelativeLayout) layoutTitle.findViewById(R.id.onelistBox); 

		ViewGroup.LayoutParams params = onelistBox.getLayoutParams();
		params.height = SinyiApplication.WindowsHeight/2;

		onelistBox.setLayoutParams(params);

		list1.setAdapter(new ServiceAdapter(list));
		list1.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				popupWindow.dismiss();
				feel.getOncliekOne("" , list.get(arg2).getName(),list.get(arg2).getNote());
			}
		});
		//这里处理
		popupWindow_view.addView(layoutTitle);

		//如果是排序,就把list置底
		if(sor){
			popupWindow_view.setGravity(Gravity.BOTTOM);
		}else{
			popupWindow_view.setGravity(Gravity.TOP);
		}


		if(with){
			setImageWanH(layoutTitle, v.getWidth());
			popupWindow_view.setGravity(Gravity.CENTER_HORIZONTAL);
		}
	}


一些查找的资料:

介绍了popupWindow 在控件的各个方向上的显示(上、下、左、右),主要用到popupWindow 的showAtLocation()方法:

在控件的上方:

[java]  view plain  copy
  1. private void showPopUp(View v) {  
  2.         LinearLayout layout = new LinearLayout(this);  
  3.         layout.setBackgroundColor(Color.GRAY);  
  4.         TextView tv = new TextView(this);  
  5.         tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  
  6.         tv.setText("I'm a pop -----------------------------!");  
  7.         tv.setTextColor(Color.WHITE);  
  8.         layout.addView(tv);  
  9.   
  10.         popupWindow = new PopupWindow(layout,120,120);  
  11.           
  12.         popupWindow.setFocusable(true);  
  13.         popupWindow.setOutsideTouchable(true);  
  14.         popupWindow.setBackgroundDrawable(new BitmapDrawable());  
  15.           
  16.         int[] location = new int[2];  
  17.         v.getLocationOnScreen(location);  
  18.           
  19.         popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], location[1]-popupWindow.getHeight());  
  20.     }  

在控件的其他方向上显示只需修改最后一行代码即可,如:

下方:popupWindow.showAsDropDown(v);

左边:

[java]  view plain  copy
  1. popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]-popupWindow.getWidth(), location[1]);  
右边:
[html]  view plain  copy
  1. popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]+v.getWidth(), location[1]);  

PopupWindow显示的方法有三个,showAsDropDown(anchor),showAsDropDown(anchor, xoff, yoff)和showAtLocation(parent, gravity, x, y)。
前两个showAsDropDown方法是让PopupWindow相对于某个控件显示,而showAtLocation是相对于整个窗口的。
第一个参数是View类型的parent,虽然这里参数名是parent,其实,不是把PopupWindow放到这个parent里,并不要求这个parent是一个ViewGroup,这个参数名让人误解。官方文档”a parent view to get the android.view.View.getWindowToken() token from
“,这个parent的作用应该是调用其getWindowToken()方法获取窗口的Token,所以,只要是该窗口上的控件就可以了。
第二个参数是Gravity,可以使用|附加多个属性,如Gravity.LEFT|Gravity.BOTTOM。
第三四个参数是x,y偏移。


待补充。。。。。。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值