反射を使用できます。次のコードをクラスに入れてから呼び出しますFoo.isInOverflow(yourMenuItem);
protected static final String SUPPORTCLASS = "android.support.v7.internal.view.menu.MenuItemImpl";
protected static final String NATIVECLASS = "com.android.internal.view.menu.MenuItemImpl";
protected static Method sSupportIsActionButton;
protected static Method sNativeIsActionButton;
static {
try {
Class<?> MenuItemImpl = Class.forName(NATIVECLASS);
sNativeIsActionButton = MenuItemImpl.getDeclaredMethod("isActionButton");
sNativeIsActionButton.setAccessible(true);
} catch (Exception ignored) {
}
try {
Class<?> MenuItemImpl = Class.forName(SUPPORTCLASS);
sSupportIsActionButton = MenuItemImpl.getDeclaredMethod("isActionButton");
sSupportIsActionButton.setAccessible(true);
} catch (Exception ignored) {
}
}
// --------------------------------------------------------------------------------------------
/**
* Check if an item is showing (not in the overflow menu).
*
* @param item
* the MenuItem.
* @return {@code true} if the MenuItem is visible on the ActionBar.
*/
public static boolean isActionButton(MenuItem item) {
switch (item.getClass().getName()) {
case SUPPORTCLASS:
try {
return (boolean) sSupportIsActionButton.invoke(item, (Object[]) null);
} catch (Exception e) {
// fall through
}
case NATIVECLASS:
try {
return (boolean) sNativeIsActionButton.invoke(item, (Object[]) null);
} catch (Exception e) {
// fall through
}
default:
return true;
}
}
/**
* Check if an item is in the overflow menu.
*
* @param item
* the MenuItem
* @return {@code true} if the MenuItem is in the overflow menu.
* @see #isActionButton(MenuItem)
*/
public static boolean isInOverflow(MenuItem item) {
return !isActionButton(item);
}
注: リフレクションが本番ビルドで機能するように、proguard 構成ファイルに次の行を追加する必要があります。
-keep public class android.support.v7.internal.view.menu.** { *; }