</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();
}
});
}
</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();
}
});
}