4

質問: MenuItem (またはどの MenuItems) が ActionBar のオーバーフロー メニューにあるかをコードでチェックする方法はありますか? 私はActionBarSherlockを使用しています

これが必要な理由は、余裕がある場合に ActionBar に表示されるアイコンがたくさんあるからです。私はホロダークテーマを持っているので、アイコンはそれに合うように作られています.

私の問題は、メニュー項目がオーバーフロー メニューに配置されたときに発生します。これは、Pre-Honeycomb デバイスでは、ユーザーがメニュー ボタンを押したときに表示されることを意味します。このメニューは、私の ActionBar とは正反対の背景であり、それに合うように別のアイコン セットが必要です。

4

4 に答える 4

3

この問題の解決策を見つけたかもしれません: デザイン ガイド (ここ) には、dip の幅に応じて表示されるアクション バー アイテムの数を示す表があります。

そのテーブルに基づいて、次のコードを作成しました。

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem search = menu.findItem(R.id.menu_search);

    // Get width in dp
    DisplayMetrics metrics = new DisplayMetrics();
    Display display = getWindowManager().getDefaultDisplay();
    display.getMetrics(metrics);
    float logicalDensity = metrics.density;
    int dp = (int) (display.getWidth() / logicalDensity + 0.5);

    if (dp < 360) { // only two icons
        search.setIcon(R.drawable.ic_menu_search);  // Show menu icon for pre-3.0 menu
    } else {
        search.setIcon(R.drawable.ic_action_search); // Show action bar icon for action bar
    }

    return true;
}
于 2012-08-27T09:37:47.920 に答える
0

あなたの問題を解決する同様の質問への回答を投稿しました。以下を参照してください。

https://stackoverflow.com/a/18884872/1299562

基本的に、onPrepareOptionsMenu を使用して非アクション アイテム アイコンを削除できます。

于 2013-09-19T01:24:25.547 に答える
0

を使用している場合はToolbar、単純な拡張Kotlin機能または静的関数を作成しJavaて、ツールバー メニュー項目がツールバーに表示されているか、オーバーフロー オプション メニューに隠されているかを確認できます。

サンプル関数とその使用法は次のとおりです。

fun Toolbar.isMenuItemOverflowing(@IdRes id: Int): Boolean {
    return this.findViewById<View>(id) == null
}

ツールバーインスタンスで関数を使用する:-

toolbar.isMenuItemOverflowing(R.id.action_search)
于 2021-12-29T06:39:01.423 に答える
0

反射を使用できます。次のコードをクラスに入れてから呼び出します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.** { *; }
于 2015-03-23T10:56:00.290 に答える