工具类
import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;
/**
* 自定义封装 PopupWindow 弹窗
*/
public class PermissionDialog {
public PopupWindow PopupWindow(Activity mContext, View view, String title, String tips) {
View inflater = LayoutInflater.from(mContext).inflate(R.layout.popup_permissions_layout, null);
PopupWindow mPopupWindow = new PopupWindow(inflater, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
mPopupWindow.setBackgroundDrawable(null);
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.setFocusable(true);
mPopupWindow.setTouchable(true);
// 设置背景颜色变暗
WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
lp.alpha = 0.7f;
mContext.getWindow().setAttributes(lp);
mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
lp.alpha = 1f;
mContext.getWindow().setAttributes(lp);
}
});
TextView tv_title = inflater.findViewById(R.id.tv_title);
tv_title.setText(title);
TextView tv_tips = inflater.findViewById(R.id.tv_tips);
tv_tips.setText(tips);
mPopupWindow.showAtLocation(view, Gravity.TOP, 0, 0);
return mPopupWindow;
}
}
xml
<?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="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="15dp"
android:layout_marginBottom="15dp"
android:background="@drawable/shape_search_bg_white"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:id="@+id/tv_title"
style="@style/MainTextDarkBold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="相机、相册权限使用说明" />
<TextView
android:id="@+id/tv_tips"
style="@style/MainTextDark"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_10"
android:text="用于更换头像" />
</LinearLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="18dp" />
<solid android:color="@color/white" />
</shape>
使用
@OnClick(R.id.tv_verify_report)
void onVerifyClick(View view) {
if (isUserLogin()) {
if (ContextCompat.checkSelfPermission(Objects.requireNonNull(getContext()), Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED) {
ActivityUtils.startActivity(ReportVerifyActivity.class);
} else {
mPopupWindow = new PermissionDialog().PopupWindow(getBaseActivity(), view, "相机权限使用说明:", "用于扫描二维码获取报告");
Disposable d = new RxPermissions(HomeFragment.this)
.request(Manifest.permission.CAMERA)
.subscribe(aBoolean -> {
if (mPopupWindow != null) {
mPopupWindow.dismiss();
}
if (aBoolean) {
ActivityUtils.startActivity(ReportVerifyActivity.class);
} else {
ToastUtils.showLong("报告验真需要开启摄像头权限");
}
});
}
}
}