1.创建一个自定义视图pupop_window.xml:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/imageView31"
android:layout_width="match_parent"
android:layout_height="136dp"
app:srcCompat="@drawable/yyyyyy1" />
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
2.在Activity或Fragment中初始化和使用PopupWindow
private void showPopupWindow(View anchorView) {
// 加载PopupWindow的布局
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup_window, null);
// 创建PopupWindow实例
final PopupWindow popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
// 设置PopupWindow的焦点和其他属性
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
// 显示PopupWindow,并指定其位置相对于anchorView(即被点击的图片)
popupWindow.showAsDropDown(anchorView);
}