1.布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_alignParentRight="true"
android:background="@drawable/mi_button"
android:layout_width="@dimen/margin120"
android:layout_height="@dimen/dp_margin_50">
<LinearLayout
android:layout_marginBottom="@dimen/dp_margin_15"
android:id="@+id/ll_friend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_margin_15"
android:gravity="center_horizontal"
android:orientation="horizontal">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@mipmap/delete_dialog" />
<TextView
android:textColor="#FFFFFF"
android:layout_width="wrap_content"
android:layout_height="@dimen/dp_margin_20"
android:layout_marginLeft="5dp"
android:gravity="center_vertical"
android:text="删除"
android:textSize="@dimen/sp_text_16" />
</LinearLayout>
</LinearLayout>
<!--<LinearLayout
android:id="@+id/ll_chat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_margin_15"
android:gravity="center_horizontal"
android:orientation="horizontal">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@mipmap/edi_dialog" />
<TextView
android:textColor="#FFFFFF"
android:layout_width="wrap_content"
android:layout_height="@dimen/dp_margin_20"
android:layout_marginLeft="@dimen/dp_margin_5"
android:gravity="center_vertical"
android:text="编辑"
android:textSize="@dimen/sp_text_16" />
</LinearLayout>-->
</RelativeLayout>
2.activity界面
private void clickpopwindow() {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.confirm_dialog, null);
ll_friend = view.findViewById(R.id.ll_friend);//删除
popwindow = new PopupWindow(view, RelativeLayout.LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
popwindow.setContentView(view);
popwindow.showAsDropDown(tv_save, Math.abs((tv_save.getWidth() - tv_save.getWidth()) / 2), 10);
DisplayMetrics d = context.getResources().getDisplayMetrics();
popwindow.setWidth((int) (d.widthPixels * 0.35));
popwindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
popwindow.setFocusable(true);
popwindow.setOutsideTouchable(true);
popwindow.update();
//实例化一个ColorDrawable颜色为半透明
ColorDrawable dw = new ColorDrawable(0x00000000);
//设置SelectPicPopupWindow弹出窗体的背景
popwindow.setBackgroundDrawable(dw);
//隐藏背景色
backgroundAlpha((Activity) context, 0.8f);//0.0-1.0
popwindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
backgroundAlpha((Activity) context, 1f);
}
});
//popwindow弹出框删除按钮
ll_friend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//判断ispopwin是否等于false,如果等于false点击弹出框
if (ispopwin==false){
tv_save.setText("取消");
tv_save.setCompoundDrawables(null, null, null, null);
mbtncheckdelete.setVisibility(View.VISIBLE);
ispopwin=true;
popwindow.dismiss();
madapter.setStatus(true);
//刷新adapter 让复选框显示
madapter.notifyDataSetChanged();
}
}
});
}
3.设置添加屏幕的背景透明度
public void backgroundAlpha(Activity context, float bgAlpha) {
WindowManager.LayoutParams lp = context.getWindow().getAttributes();
lp.alpha = bgAlpha;
context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
context.getWindow().setAttributes(lp);
}