如何检查PopupMenu现在显示的操作溢出?
当我尝试自动隐藏ActionBar时需要我,并且在ActionBar的溢出弹出窗口显示时不需要隐藏。
答案 0 :(得分:4)
您可以覆盖Activity.onMenuOpened
和Activity.onPanelClosed
以确定何时显示溢出菜单或其他子菜单,例如ShareActionProvider
。这是一个例子:
/** True if an options menu has been opened, false otherwise */
private boolean mMenuOpened;
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
mMenuOpened = true;
return super.onMenuOpened(featureId, menu);
}
@Override
public void onPanelClosed(int featureId, Menu menu) {
super.onPanelClosed(featureId, menu);
mMenuOpened = false;
}
<强>替代地强>
ActionBarView
子类AbsActionBarView
,其中包含AbsActionBarView.isOverflowMenuShowing
。由于这些类是内部的并且是隐藏的,因此您需要通过反射来访问它。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final int actionBarViewId = getResources().getIdentifier("action_bar", "id", "android");
final View actionBarView = findViewById(actionBarViewId);
actionBarView.postDelayed(new Runnable() {
@Override
public void run() {
System.out.println(isOverflowMenuShowing(actionBarView));
}
}, 2500);
}
private static boolean isOverflowMenuShowing(View actionBarView) {
try {
final Class<?> abv = Class.forName("com.android.internal.widget.ActionBarView");
final Method isOverflowShowing = abv.getMethod("isOverflowMenuShowing", new Class[] {});
return (boolean) isOverflowShowing.invoke(actionBarView, new Object[] {});
} catch (final Exception ignored) {
// Nothing to do
}
return false;
}