效果图
![]()
1、布局文件 layout_popup.xml
<ListView android:id="@+id/popup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="@android:color/transparent"/>
layout_popup_list.xml
<LinearLayout
android:id="@+id/item"
android:layout_width="match_parent"
android:layout_height="@dimen/space_40"
android:background="@drawable/block_item_default_selector">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="@dimen/font_main_size"
android:textColor="@color/font_main_color"/>
</LinearLayout>
2.java文件
private void showPopupWindow(View view) {
// 一个自定义的布局,作为显示的内容
View contentView = LayoutInflater.from(this).inflate(
R.layout.layout_popup, null);
//状态选择
String status = "yespwd";
ListView popuplist = (ListView)contentView.findViewById(R.id.popup);
data = getData(status);
SimpleAdapter simpleAdapter = new SimpleAdapter(this,data,
R.layout.layout_popup_list, new String[] {"text"},
new int[] {R.id.text});
popuplist.setAdapter(simpleAdapter);
final PopupWindow popupWindow = new PopupWindow(contentView,
200, ViewGroup.LayoutParams.WRAP_CONTENT, true);
//点击外部后消失
popupWindow.setTouchable(true);
popuplist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String text = (String) data.get(i).get("text");
if (text.equals("status")) {
Intent intent = new Intent(PrePaymentActivity.this, PaymentHistoryActivity.class);
startActivity(intent);
//弹框消失
popupWindow.dismiss();
}
}
});
popupWindow.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
// 这里如果返回true的话,touch事件将被拦截
// 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss
}
});
// 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
popupWindow.setBackgroundDrawable(getResources().getDrawable(
R.color.white));
// 设置好参数之后再show
popupWindow.showAsDropDown(view);
}
//输入数据
private List<Map<String, Object>> getData(String stutas)
{
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map,map1,map2,map3;
if(stutas.equals("yespwd")){
map = new HashMap<String, Object>();
map.put("text", "1");
map1 = new HashMap<String, Object>();
map1.put("text", "2");
map2 = new HashMap<String, Object>();
map2.put("text", "3");
list.add(map);
list.add(map1);
list.add(map2);
}
return list;
}