菜单项未显示在操作栏上

53

我制作了一个全新的项目。我已经向menu布局文件添加了项目,但这些项目没有显示在操作栏的右侧。我记得会出现三个点的图标,点击后会打开菜单。

enter image description here

这是我的Activity

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar actionBar = getActionBar();
        actionBar.setBackgroundDrawable(new ColorDrawable(Color.BLUE));     
        actionBar.show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

这是我的main.xml文件:

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

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_option1"/>
    <item
        android:id="@+id/action_settings34"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_option2"/>
    <item
        android:id="@+id/action_settings3"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_option3"/>


</menu>

这是在哪个设备上发生的?另外,你能给我们展示一些代码吗?这很相关。 - gunar
你可以在这里发布你的代码吗? - Sanket Shah
抱歉,我忘记了代码。@David - prometheuspk
设备是HTC One V。我正在运行CyanogenMod 10。我已经更新了文本@gunar。 - prometheuspk
1
http://stackoverflow.com/questions/16196368/android-want-to-show-3-dots-menu-on-ics - Raghunandan
你的安卓操作系统版本是多少? - Sanket Shah
13个回答

0

在showAsAction和actionViewClass中添加自定义命名空间,如下所示

<menu xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/search"
    android:title="@string/search"
    android:icon="@drawable/ic_search"
    app:showAsAction="collapseActionView|ifRoom"
    app:actionViewClass="android.widget.SearchView" />

0
你需要在活动的onCreate函数中调用此函数。
RequestWindowFeature (Window.FEATURE_ACTION_BAR);

1
请尽量正确拼写您的句子,以保持Stackoverflow上问题/答案的高质量。 - Thomas Baruchel

0

以上所有答案都很好地解释了这个问题

但是我们能否覆盖这个限制并有溢出菜单项?!

是的,我们可以进行一些技巧操作。

 private void makeActionOverflowMenuShown() {
    //devices with hardware menu button (e.g. Samsung Note) don't show action overflow menu
    try {
        ViewConfiguration config = ViewConfiguration.get(this);
        Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
        if (menuKeyField != null) {
            menuKeyField.setAccessible(true);
            menuKeyField.setBoolean(config, false);
        }
    } catch (Exception e) {
        Log.d(TAG, e.getLocalizedMessage());
    }
}

在 onCreate 中调用它

参考链接:https://dev59.com/ZGkw5IYBdhLWcg3wp8Sc#13098824


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接