PopupWindow 与 ActionBar 重叠
PopupWindow overlaps ActionBar
我正在使用 PopupWindow 通过 showAsDropDown(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>
尝试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.
您也可以尝试使用 popup.setOverlapAnchor(false)
Sets whether the popup window should overlap its anchor view when displayed as a drop-down.
希望对您有所帮助。
尝试同时减去操作栏和状态栏的高度:
popup.setHeight(WindowManager.LayoutParams.WRAP_CONTENT - actionBarHeight - statusBarHeight);
我的情况是,我使用 FrameLayout 父级以编程方式创建了弹出窗口。
PopupWindow popupWindow = new PopupWindow(frameLayout, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT - actionBarHeight - statusBarHeight);
试试这些!
第 1 次隐藏键盘,因此顶部内容可见。
public void hideSoftKeyboard(View view){
InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
然后,如果您使用的是滚动视图,请滚动到要显示弹出窗口的位置(EditText/Other 视图)!
View targetView = findViewById(R.id.DESIRED_VIEW_ID);
targetView.getParent().requestChildFocus(targetView,targetView);
参考:
或另一个参考:
尝试在调用 showPopup(...)
之前调用 editText.requestFocus()
,如果有效请告诉我。
我正在使用 PopupWindow 通过 showAsDropDown(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>
尝试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.
您也可以尝试使用 popup.setOverlapAnchor(false)
Sets whether the popup window should overlap its anchor view when displayed as a drop-down.
希望对您有所帮助。
尝试同时减去操作栏和状态栏的高度:
popup.setHeight(WindowManager.LayoutParams.WRAP_CONTENT - actionBarHeight - statusBarHeight);
我的情况是,我使用 FrameLayout 父级以编程方式创建了弹出窗口。
PopupWindow popupWindow = new PopupWindow(frameLayout, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT - actionBarHeight - statusBarHeight);
试试这些!
第 1 次隐藏键盘,因此顶部内容可见。
public void hideSoftKeyboard(View view){
InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
然后,如果您使用的是滚动视图,请滚动到要显示弹出窗口的位置(EditText/Other 视图)!
View targetView = findViewById(R.id.DESIRED_VIEW_ID);
targetView.getParent().requestChildFocus(targetView,targetView);
参考:
或另一个参考:
尝试在调用 showPopup(...)
之前调用 editText.requestFocus()
,如果有效请告诉我。