private void showSubjectPopupWindow(View view) {//想要选择框在哪个view下面,就把那个view作为参数传递进来 // 一个自定义的布局,作为显示的内容 View contentView = LayoutInflater.from(mActivity).inflate(R.layout.pop_home_title,null); TextView tvOverdue = (TextView) contentView.findViewById(R.id.tvOverdue); TextView tvYesDeal = (TextView) contentView.findViewById(R.id.tvYesDeal); TextView tvNoDeal = (TextView) contentView.findViewById(R.id.tvNoDeal); TextView popDis = (TextView) contentView.findViewById(R.id.popDis); tvOverdue.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ToastUtil.showToast(mActivity,"已经过期"); } }); tvNoDeal.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ToastUtil.showToast(mActivity,"未处理"); } }); tvYesDeal.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ToastUtil.showToast(mActivity,"已处理"); } }); popDis.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { popupWindow.dismiss(); } }); popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true); //实例化一个ColorDrawable颜色为半透明,已达到变暗的效果 ColorDrawable dw = new ColorDrawable(0xb0000000); // 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框 // 我觉得这里是API的一个bug popupWindow.setBackgroundDrawable(dw); // 设置好参数之后再show popupWindow.showAsDropDown(view, 0, 0); }
布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:background="#3f000000" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:background="#fff" android:layout_height="80px"> <TextView android:id="@+id/tvNoDeal" android:gravity="center" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="未处理"/> <View android:layout_width="2px" android:layout_height="match_parent" android:background="#eee"/> <TextView android:id="@+id/tvYesDeal" android:gravity="center" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="已处理"/> <View android:layout_width="2px" android:layout_height="match_parent" android:background="#eee"/> <TextView android:id="@+id/tvOverdue" android:gravity="center" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="过期"/> </LinearLayout> <TextView android:id="@+id/popDis" android:background="#3f000000" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>