Android弹出框效果——popupwindow

</pre>什么是popupwindows</h1><div><span style="font-family:Verdana,Arial,Helvetica,sans-serif; font-size:15px; line-height:27px"> </span><span style="font-family:SimSun"><span style="font-size:15px; line-height:27px"> PopupWindow是一个有弹出框的效果的类,</span><span style="font-size:15px; line-height:27px"><span style="background-color:rgb(255,255,255)"><span style="color:#cc66cc"><strong>可以使用任意布局的View作为其内容</strong></span></span></span><span style="font-size:15px; line-height:27px">,而且这个弹出框是<span style="color:#cc33cc"><strong>悬浮在当前activity之上</strong></span>的。但是它跟<span style="font-size:16px; line-height:24px">AlertDialog还是有区别的。<span style="font-family:SimSun; font-size:16px; line-height:24px">AlertDialog弹出位置是固定的,而popupwindow却可以随意显示,从灵活性上来讲,我更喜欢popupwindows。</span></span></span></span></div><h5><span style="font-family:SimHei; font-size:24px"><span style="line-height:24px">popupwindows的实现</span></span></h5><div><span style="font-family:SimHei; font-size:24px"><span style="line-height:24px"><img src="https://img-blog.csdn.net/20150729151517972?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /></span></span></div><div><span style="font-family:SimSun; font-size:14px"><span style="line-height:24px">不多说,先上代码。</span></span></div><div><pre name="code" class="html">private void showPopupWindow(View parent) {
		if (popupWindow == null) {
			// 一个自定义的布局,作为显示的弹窗界面
			final View contentView = LayoutInflater.from(context).inflate(
					R.layout.popwindows, null);
			// 设置按钮的点击事件
			list1 = (ListView) contentView.findViewById(R.id.list1);//一级列表
			list2 = (ListView) contentView.findViewById(R.id.list2);//二级列表
			list1.setAdapter(new ArrayAdapter<String>(this,
					android.R.layout.simple_list_item_multiple_choice, groups));
			popupWindow = new PopupWindow(contentView, 600,
					590, true);
		}
		// 设置允许在外点击消失
		popupWindow.setOutsideTouchable(true);
		popupWindow.setTouchable(true);

		popupWindow.setTouchInterceptor(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {

<span style="white-space:pre">				</span>//该函数内可以写当点击弹窗外围时触发的事件
				return false;
			}
		});

		// 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		
		// 设置好参数之后再show,这里可以设置弹窗显示的位置,默认是从父控件的左下角开始
		popupWindow.showAsDropDown(parent);

		list1.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				//一级列表的处罚事件,可以用来设置二级列表的显示内容
			}
		});

		list2.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				list2.setVisibility(View.GONE);
				popupWindow.dismiss();
			}
		});

	}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值