Android的 - 取下动作条“更多操作”按钮(Android - Remove “More ac

2019-07-29 19:28发布

我应该在自定义的方式显示操作按钮的动作条。 为此,我创建了一个自定义视图,并将其连接到动作条。

有一点要提的是,我使用的是menu.xml文件 resoure文件来加载选项菜单,并在智能手机上显示出来,但不要对平板显示出来,而不是使用自定义视图。 为此,我的市场在XML,因为每个菜单项: android:showAsAction="never"

一切都看起来不错,但仍保留在动作条右侧的一个小东西 - “更多”按钮。

我怎样才能删除它?

我尝试这样做:

ActionBar bar = activity.getActionBar();
bar.removeAllTabs();

但“更多”按钮仍然存在。

编辑:
这是我menu.xml文件的文件:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/menu_username"
        android:icon="@drawable/menu_username"
        android:orderInCategory="0"
        android:showAsAction="never"
        android:title="@string/menu_username">
        <menu>
            <item
                android:id="@+id/menu_logout"
                android:title="@string/menu_logout"/>
        </menu>
    </item>

    <item
        android:id="@+id/menu_settings"
        android:icon="@drawable/menu_settings"
        android:orderInCategory="1"
        android:showAsAction="never"
        android:title="@string/menu_settings"/>

    <item
        android:id="@+id/menu_search"
        android:icon="@drawable/menu_search"
        android:orderInCategory="1"
        android:showAsAction="never"
        android:title="@string/menu_search"/>

</menu>

请注意,我还是要膨胀在智能手机上这个菜单,但不希望使用它在平板电脑上。

Answer 1:

设置showAsAction="never"将强制菜单项进入溢流。 为什么在onCreateOptionsMenu不检查(...),该设备是平板,如果它是不膨胀的菜单吗? 事情是这样的:

public boolean onCreateOptionsMenu(Menu menu) {
    if (getResources().getConfiguration().smallestScreenWidthDp >= 600) {
        //It's a tablet, don't inflate, only create the manual view
        manualMenuCreation();
    } else {
        getMenuInflater().inflate(R.menu.menu, menu);
    }
    return true;
}

不要忘记smallestScreenWidthDp仅在3.2或以上,所以你就必须考虑到这一点。



文章来源: Android - Remove “More actions” button from the ActionBar