所以這個問題已經讓我發瘋了很長時間,我在任何地方都找不到解決方案。基本上我有一個對話框片段,頂部有兩個浮動操作按鈕。右邊的,當點擊時,創建這個彈出選單。到現在為止還挺好。但是正如您從圖片中看到的那樣,彈出選單打破了導致導航欄出現的沉浸式模式,并且由于某種原因,選單中的最后一個元素沒有被剪裁并被繪制在其下方。這顯然是一個問題,因為如果您嘗試單擊它,您最終會與導航欄進行互動。

這是我的按鈕和彈出選單的代碼:
notificationSchedule.setOnClickListener(v -> {
PopupMenu notificationMenu = new PopupMenu(requireActivity(), v);
notificationMenu.inflate(R.menu.notification_changer);
SpannableString spannableString1 = new SpannableString(notificationMenu.getMenu().getItem(databaseNotification.getIndex1()).getTitle());
spannableString1.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorPrimary)), 0, spannableString1.length(), 0);
notificationMenu.getMenu().getItem(databaseNotification.getIndex1()).setTitle(spannableString1);
if (notificationMenu.getMenu().getItem(databaseNotification.getIndex1()).getSubMenu() != null) {
spannableString1 = new SpannableString(notificationMenu.getMenu().getItem(databaseNotification.getIndex1()).getSubMenu().getItem(databaseNotification.getIndex2()).getTitle());
spannableString1.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorPrimary)), 0, spannableString1.length(), 0);
notificationMenu.getMenu().getItem(databaseNotification.getIndex1()).getSubMenu().getItem(databaseNotification.getIndex2()).setTitle(spannableString1);
}
notificationMenu.show();
notificationMenu.setOnMenuItemClickListener(item -> {
boolean value = managePopupMenuClick(-1, item);
if (notificationMenu.getMenu().findItem(item.getItemId()).getSubMenu() != null) {
SpannableString spannableString2 = new SpannableString(notificationMenu.getMenu().findItem(item.getItemId()).getTitle() " ");
spannableString2.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorPrimary)), 0, spannableString2.length(), 0);
notificationMenu.getMenu().findItem(item.getItemId()).getSubMenu().setHeaderTitle(spannableString2);
}
return value;
});
});
到目前為止我嘗試過的:
1.我最接近解決這個問題的是在我的onCreateView()方法中使用這種和平的代碼:
dialogWindow.getDecorView().setOnSystemUiVisibilityChangeListener(visibility -> {
if (visibility == 4) dialogWindow.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
});
From what I have tested this fixes the problem but on very few devices.
2. I tried hiding the navigation bar immediately after the popup menu is shown but that didn't work as well.
3. I searched for a way to maintain the immersive mode while showing the popup menu , but couldn't find a way to do it.
4. I also searched for a way to try and limit the number of visible items in the popup menu. For example as you can see there are 11 items in this popup menu. If there was a way to limit the number of visible items to 4 for example and then scroll to see the other ones that would be a fix as well, but I again couldn't find a way to do it.
Any suggestions would be appreciated!
uj5u.com熱心網友回復:
免責宣告
這不是問題的直接解決方案,但您可以將其用作最后的手段。
如果您注意到您的彈出選單向螢屏底部下拉,原因是錨視圖位于螢屏的上半部分。
如果您將錨點更改為底部的其他視圖(可能您需要創建一些不可見的視圖),則彈出選單將朝螢屏頂部打開;在這種情況下,它永遠不會與底部導航欄相交。
uj5u.com熱心網友回復:
- 彈出選單打破了導致導航欄出現的沉浸式模式。
我認為這是 Android UI 的預期規范。當選單彈出時,您的視窗失去焦點,隱藏的導航欄重新出現。
- 出于某種原因,選單中的最后一個元素沒有被剪裁并被繪制在它下面。這顯然是一個問題,因為如果您嘗試單擊它,您最終會與導航欄進行互動。
導航欄位于彈出視窗上方。我再次認為這是有意的。例如,您可以導航回之前的狀態(關閉彈出視窗等)。
您可以手動向上滾動彈出選單并避免這種重疊狀態。

uj5u.com熱心網友回復:
我想我終于知道是怎么回事了。有幾件事情需要重新設計。因此,對于遇到此問題的其他人,請嘗試以下操作:
1.我有一種隱藏系統UI的方法和一種顯示它的方法。
private void hideSystemUI() {
dialogWindow.getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
private void showSystemUI() {
dialogWindow.getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
注意 1:事實證明,標志View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION導致了問題,您可以從標志描述中閱讀。
2.我最終從onCreateView() 中洗掉了這段代碼:
dialogWindow.getDecorView().setOnSystemUiVisibilityChangeListener(visibility -> {
if (visibility == 4) dialogWindow.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
});
注意 2:但事實證明,需要清除這些標志。所以這段代碼dialogWindow.clearFlags(...); 將不得不使用。
所以我的onClickListener()的最終版本看起來像這樣(在每個注釋塊下面都有代碼更改):
notificationSchedule.setOnClickListener(v -> {
// Show the system UI before creating the popup menu
// but without the View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION flag !!!
dialogWindow.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
// Instead of having a setOnSystemUiVisibilityChangeListener() in the onCreateView()
// clear the flags here !!!
dialogWindow.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
// I also ended up using another constructor for the popup menu because I wanted
// to use the R.attr.actionOverflowMenuStyle
// This is not needed, but if you do want to use it know that it requires API 22
PopupMenu notificationMenu;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
notificationMenu = new PopupMenu(requireActivity(), v, Gravity.NO_GRAVITY, R.attr.actionOverflowMenuStyle, 0);
} else {
notificationMenu = new PopupMenu(requireActivity(), v);
}
notificationMenu.inflate(R.menu.notification_changer);
SpannableString spannableString1 = new SpannableString(notificationMenu.getMenu().getItem(databaseNotification.getIndex1()).getTitle());
spannableString1.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorPrimary)), 0, spannableString1.length(), 0);
notificationMenu.getMenu().getItem(databaseNotification.getIndex1()).setTitle(spannableString1);
if (notificationMenu.getMenu().getItem(databaseNotification.getIndex1()).getSubMenu() != null) {
spannableString1 = new SpannableString(notificationMenu.getMenu().getItem(databaseNotification.getIndex1()).getSubMenu().getItem(databaseNotification.getIndex2()).getTitle());
spannableString1.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorPrimary)), 0, spannableString1.length(), 0);
notificationMenu.getMenu().getItem(databaseNotification.getIndex1()).getSubMenu().getItem(databaseNotification.getIndex2()).setTitle(spannableString1);
}
notificationMenu.show();
notificationMenu.setOnMenuItemClickListener(item -> {
boolean value = managePopupMenuClick(-1, item);
if (notificationMenu.getMenu().findItem(item.getItemId()).getSubMenu() != null) {
SpannableString spannableString2 = new SpannableString(notificationMenu.getMenu().findItem(item.getItemId()).getTitle() " ");
spannableString2.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorPrimary)), 0, spannableString2.length(), 0);
notificationMenu.getMenu().findItem(item.getItemId()).getSubMenu().setHeaderTitle(spannableString2);
}
return value;
});
// Finally add a onDismissListener() and hide the system UI inside it
notificationMenu.setOnDismissListener(menu -> hideSystemUI());
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/326273.html
標籤:android android-layout android-dialogfragment popupmenu
