居中PopupWindow的简单封装

xiaoxiao2021-02-28  6

居中显示的PopupWindow,传入Activity,设置标题和内容,子类需要实现clickConfirm方法。MyApplication为继承自Application的类,getContext()方法=getApplicationContext();

一、PopWindowTip代码

/** * PopupWindow显示需要的参数:activity,pop的标题以及内容 */ public abstract class PopWindowTip { PopupWindow mPopTip; private Context context = MyApplication.getContext(); private Activity activity; private String title; private String content; private View contentView; public PopWindowTip() { mPopTip = new PopupWindow(); } public void setTitleAndContent(String title, String content) { this.title = title; this.content = content; mPopTip.showAtLocation(contentView, Gravity.CENTER, 0, 0); TextView pop_title = contentView.findViewById(R.id.pop_title); TextView pop_message = contentView.findViewById(R.id.pop_message); Button btn_cancel = contentView.findViewById(R.id.btn_cancel); Button btn_submit = contentView.findViewById(R.id.btn_submit); pop_title.setText(title); pop_message.setText(content); UIUtils.darkenBackgroud(activity, 0.5f); btn_cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mPopTip.dismiss(); } }); btn_submit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { clickConfirm(); mPopTip.dismiss(); } }); } public PopWindowTip(final Activity activity) { this.activity = activity; LayoutInflater inflater = activity.getLayoutInflater(); this.contentView = inflater.from(activity).inflate(R.layout.pop_tip, null); mPopTip = new PopupWindow(contentView, activity.getWindowManager().getDefaultDisplay().getWidth() - 200, WindowManager.LayoutParams.WRAP_CONTENT, true); mPopTip.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { UIUtils.darkenBackgroud(activity, 1.0f); } }); } public abstract void clickConfirm(); }

二、布局文件pop_tip.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:background="@drawable/bg_poptip" android:padding="15dp" android:orientation="vertical" android:layout_height="wrap_content"> <TextView android:id="@+id/pop_title" android:layout_marginTop="5dp" android:layout_marginLeft="@dimen/dp_10" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="弹窗标题" android:textSize="18sp" android:textColor="@android:color/black" /> <TextView android:layout_marginLeft="15dp" android:layout_marginRight="18dp" android:id="@+id/pop_message" android:layout_marginTop="12dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="删除账号后将无法找回,确定删除该账号?" android:textSize="17sp" /> <LinearLayout android:layout_width="match_parent" android:gravity="right" android:layout_marginTop="12dp" android:layout_marginRight="5dp" android:layout_height="wrap_content"> <Button android:id="@+id/btn_cancel" android:layout_marginRight="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="@dimen/sp_16" android:text="取消" android:textColor="@android:color/darker_gray" android:background="@null" android:minHeight="26dp" android:minWidth="@dimen/dp_40" /> <Button android:id="@+id/btn_submit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="@dimen/sp_16" android:text="确定" android:textColor="@color/colorPrimary" android:background="@null" android:minHeight="26dp" android:minWidth="@dimen/dp_40" /> </LinearLayout> </LinearLayout>

三、PopupWindow背景drawable

bg_poptip.xml

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 设置填充颜色 --> <solid android:color="@android:color/white" /> <!-- 设置几何图形四个角的弧度 --> <corners android:radius="6dp"/> </shape>

四、在Activity里面使用

PopWindowTip popTip = new PopWindowTip(MainActivity.this){ @Override public void clickConfirm() { //To Do } }; popTip.setTitleAndContent("删除警告", "账号被删除之后将无法被找回,确定删除该账号?");

五、效果

转载请注明原文地址: https://www.6miu.com/read-2000142.html

最新回复(0)