PopupWindow与ActionBar重叠

问题描述 投票:1回答:2

我正在使用PopupWindow通过showAsDropDown(anchor)进行显示验证错误。通过按保存按钮来验证验证字段,因此如果将锚点放置在操作栏下,则其弹出窗口会与actionBar重叠。我该如何解决?

Example

 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>
java android android-layout popup popupwindow
2个回答
0
投票

尝试popup.setAttachedInDecor(true)

这会将弹出窗口附加到父窗口的装饰框架,以避免与导航栏之类的屏幕装饰重叠。

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

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

希望有帮助。


0
投票

尝试同时减去操作和状态栏的高度:

popup.setHeight(WindowManager.LayoutParams.WRAP_CONTENT - actionBarHeight - statusBarHeight);

我的情况是,我使用FrameLayout父级以编程方式创建了popupwindow。

PopupWindow popupWindow = new PopupWindow(frameLayout, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT - actionBarHeight - statusBarHeight);
© www.soinside.com 2019 - 2024. All rights reserved.