简体   繁体   English

PopupWindow 与 ActionBar 重叠

[英]PopupWindow overlaps ActionBar

I am using PopupWindow for show validation error with showAsDropDown(anchor).Validation Fields are validated by pressing the save button, so If anchor place under action bar, its popup overlaps actionBar.我正在使用 PopupWindow 显示验证错误和 showAsDropDown(anchor).Validation 字段是通过按下保存按钮来验证的,所以如果锚点放在操作栏下,它的弹出窗口会与 actionBar 重叠。 How do I fix this?我该如何解决?

例子

 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();
    }
}

Popup for validation error XML:验证错误 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>

Try popup.setAttachedInDecor(true)试试popup.setAttachedInDecor(true)

This will attach the popup window to the decor frame of the parent window to avoid overlaping with screen decorations like the navigation bar.这会将弹出窗口附加到父窗口的装饰框架,以避免与导航栏等屏幕装饰重叠。

Also you may try using popup.setOverlapAnchor(false)您也可以尝试使用popup.setOverlapAnchor(false)

Sets whether the popup window should overlap its anchor view when displayed as a drop-down.设置弹出窗口在显示为下拉菜单时是否与其锚视图重叠。

Hope it helps.希望能帮助到你。

try to subtract both action and status bar height :尝试减去动作和状态栏的高度:

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

my case, i created popupwindow programmatically with a FrameLayout parent.在我的情况下,我使用 FrameLayout 父级以编程方式创建了 popupwindow。

PopupWindow popupWindow = new PopupWindow(frameLayout, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT - actionBarHeight - statusBarHeight);

Try these !试试这些!

1st hide keyboard, so the top content will be visible.第一个隐藏键盘,因此顶部内容将可见。

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

Then if you are using scrollview scroll to where (EditText/Other View) you want to show popup !然后,如果您使用滚动视图滚动到您想要显示弹出窗口的位置(EditText/Other View)!

View targetView = findViewById(R.id.DESIRED_VIEW_ID);  
targetView.getParent().requestChildFocus(targetView,targetView);

Ref: https://stackoverflow.com/a/31764551/5557479参考: https : //stackoverflow.com/a/31764551/5557479

or another ref : https://stackoverflow.com/a/35627860/5557479或其他参考: https : //stackoverflow.com/a/35627860/5557479

在调用showPopup(...) editText.requestFocus()之前尝试调用editText.requestFocus()并让我知道它是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM