Android选项菜单未在操作栏4.1版中显示

时间:2015-03-04 07:02:01

标签: android android-actionbar

我正在创建一个非常简单的操作栏。我刚刚在菜单中添加了2个项目,但它仍不适用于Android 4.1。它确实适用于nexus 5模拟器。

menu_main.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=".MainActivity" >

    <item
        android:id="@+id/action_settings"
        android:title="@string/action_settings"
        app:showAsAction="never"/>
    <item
        android:id="@+id/action_exit"
        android:title="Exit"
        app:showAsAction="never"/>

</menu>

MainActivity.java

package com.mycompany.actionbaractivitypractise;


import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 } 


 @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_main, menu);
    return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
 }
}

任何意见或建议都会有所帮助。谢谢

1 个答案:

答案 0 :(得分:-1)

你的java代码是正确的但是在menu_menu.xml而不是app:使用android它会工作:
例如:

<item
    android:id="@+id/action_settings"
    android:title="@string/action_settings"
    android:showAsAction="never"/>
<item
    android:id="@+id/action_exit"
    android:title="Exit"
    android:showAsAction="never"/>