弹出窗口覆盖了ActionBar

7

我正在使用 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>

请添加 XML 代码。 - Juanje
“ActionBar”不会重叠内容。但是带有文本“属性必须有效…”的弹出窗口会重叠所有内容。 - kalugin1912
4个回答

3

尝试使用 popup.setAttachedInDecor(true)

这将使弹出窗口附着在父窗口的装饰框架上,避免与屏幕装饰(如导航栏)重叠。

您还可以尝试使用 popup.setOverlapAnchor(false)

设置弹出窗口在显示为下拉菜单时是否应重叠其锚视图。

希望能帮到您。


1
尝试减去操作栏和状态栏的高度:
popup.setHeight(WindowManager.LayoutParams.WRAP_CONTENT - actionBarHeight - statusBarHeight);

我创建了一个带有FrameLayout父容器的程序化弹出窗口。
PopupWindow popupWindow = new PopupWindow(frameLayout, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT - actionBarHeight - statusBarHeight);

如果导航抽屉已打开,则此情况将无法解决问题。 - kalugin1912

1

请试试以下方法!

首先,隐藏键盘,这样顶部的内容就会显示出来。

public void hideSoftKeyboard(View view){
  InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
  imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

如果您正在使用ScrollView,请滚动到您想要显示弹出窗口的(EditText/其他视图)位置!
View targetView = findViewById(R.id.DESIRED_VIEW_ID);  
targetView.getParent().requestChildFocus(targetView,targetView);

参考:https://dev59.com/emEi5IYBdhLWcg3wN6Nt#31764551

或者另一个参考:https://dev59.com/emEi5IYBdhLWcg3wN6Nt#35627860


屏幕上有很多EditText,因此隐藏键盘无法解决这个问题。 - kalugin1912
首先滚动到弹出窗口的位置,然后显示弹出窗口! - Naveen Avidi

0
尝试在调用showPopup(...)之前调用editText.requestFocus(),并告诉我它是否有效。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接