无法在Android中看到菜单

时间:2017-11-02 15:36:50

标签: android menu

我是android的初学者。 我正在尝试为培训应用创建菜单。 我不知道我的菜单没有印刷。

我在res / menu / menu / home.xml上有一个简单的菜单

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.gon.myapplication.HomeActivity">
<item
    android:id="@+id/action_settings"
    android:orderInCategory="1"
    android:title="@string/app_name"
    app:showAsAction="always" />

在我的活动上我尝试显示它

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

我做错了吗?谢谢你的帮助。

2 个答案:

答案 0 :(得分:0)

First, make sure your activity has an action bar or a Toolbar as an action bar.

And use return super.onCreateOptionsMenu(menu); instead of return true;.

Also consider using app:showAsAction="never" in your menu item.

答案 1 :(得分:0)

试试这个:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Add this line of code
    super.onCreateOptionsMenu(menu);

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_home, menu);
    return true;
}

修改

好的,如果您忘记关闭带有结束标记的菜单,请执行以下操作:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 tools:context="com.example.gon.myapplication.HomeActivity">
 <item
android:id="@+id/action_settings"
android:orderInCategory="1"
android:title="@string/app_name"
app:showAsAction="always" />

 //add this

 </menu>

如果这不是你的错误,那么用这个

替换你的xml文件
<menu xmlns:android="http://schemas.android.com/apk/res/android"
  >
<item
android:id="@+id/action_settings"
android:title="@string/app_name"
/>


</menu>

并保持java代码完全按照我上面告诉你的方式。