我正在使用 PopupWindow 来显示验证错误信息,使用 showAsDropDown(anchor) 方法。这些表单字段在按下保存按钮时进行验证,如果 anchor 位于操作栏下方,则会弹出窗口覆盖操作栏。我该如何解决这个问题?
protected void showPopup(View anchorPlace) {
popupContainer.removeAllViews();
popupContainer.addView(recyclerErrors);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
popupContainer.setElevation(0);
anchorPlace.setElevation(0);
popup.setElevation(0);
}
recyclerErrors.setOnClickListener(v -> dismissPopup());
popup.setContentView(popupContainer);
if (anchorPlace != null) {
popup.setWidth(anchorPlace.getWidth());
}
popup.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popup.setOutsideTouchable(true);
popup.setFocusable(false);
popup.setTouchable(true);
popup.setBackgroundDrawable(null);
if (anchorPlace != null) {
PopupWindowCompat.showAsDropDown(popup, anchorPlace, 0, 0, Gravity.BOTTOM);
}
if (popup.isAboveAnchor()) {
popup.dismiss();
}
}
验证错误XML的弹出窗口:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
app:srcCompat="@drawable/warning_triangle" />
<TextView
android:id="@+id/error_field_error_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/warning_bcg"
android:drawableStart="@drawable/ic_warning"
android:drawablePadding="@dimen/settings_error_body_padding_top_bottom"
android:gravity="center_vertical"
android:paddingStart="@dimen/settings_error_body_padding_start_end"
android:paddingTop="@dimen/settings_error_body_padding_top_bottom"
android:paddingEnd="@dimen/settings_error_body_padding_start_end"
android:paddingBottom="@dimen/settings_error_body_padding_top_bottom"
android:textColor="@android:color/white"
android:textSize="@dimen/settings_error_text_size"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/></LinearLayout>