Merge "Fix icon spacing on both MenuPopup implementations." into nyc-dev

This commit is contained in:
Oren Blasberg
2016-04-07 16:37:46 +00:00
committed by Android (Google) Code Review
3 changed files with 31 additions and 2 deletions

View File

@@ -346,7 +346,15 @@ final class CascadingMenuPopup extends MenuPopup implements MenuPresenter, OnKey
private void showMenu(@NonNull MenuBuilder menu) {
final LayoutInflater inflater = LayoutInflater.from(mContext);
final MenuAdapter adapter = new MenuAdapter(menu, inflater, mOverflowOnly);
adapter.setForceShowIcon(mForceShowIcon);
// Apply "force show icon" setting; if the menu being shown is the top level menu, apply the
// setting set on this CascadingMenuPopup by its creating code. If it's a submenu, or if no
// "force" setting was explicitly set, determine the setting by examining the items.
if (!isShowing() && mForceShowIcon) {
adapter.setForceShowIcon(mForceShowIcon);
} else {
adapter.setForceShowIcon(MenuPopup.shouldPreserveIconSpacing(menu));
}
final int menuWidth = measureIndividualMenuWidth(adapter, null, mContext, mMenuMaxWidth);
final MenuPopupWindow popupWindow = createPopupWindow();

View File

@@ -183,4 +183,25 @@ public abstract class MenuPopup implements ShowableListMenu, MenuPresenter,
}
return (MenuAdapter) adapter;
}
/**
* Returns whether icon spacing needs to be preserved for the given menu, based on whether any
* of its items contains an icon.
* @param menu
* @return Whether to preserve icon spacing.
*/
protected static boolean shouldPreserveIconSpacing(MenuBuilder menu) {
boolean preserveIconSpacing = false;
final int count = menu.size();
for (int i = 0; i < count; i++) {
MenuItem childItem = menu.getItem(i);
if (childItem.isVisible() && childItem.getIcon() != null) {
preserveIconSpacing = true;
break;
}
}
return preserveIconSpacing;
}
}

View File

@@ -266,7 +266,7 @@ final class StandardMenuPopup extends MenuPopup implements OnDismissListener, On
final MenuPopupHelper subPopup = new MenuPopupHelper(mContext, subMenu,
mShownAnchorView, mOverflowOnly, mPopupStyleAttr, mPopupStyleRes);
subPopup.setPresenterCallback(mPresenterCallback);
subPopup.setForceShowIcon(mAdapter.getForceShowIcon());
subPopup.setForceShowIcon(MenuPopup.shouldPreserveIconSpacing(subMenu));
// Pass responsibility for handling onDismiss to the submenu.
subPopup.setOnDismissListener(mOnDismissListener);