首页 > 解决方案 > 菜单不会显示。我不知道为什么

问题描述

我只是在关注另一个教程,但结果不同。当我尝试运行我的 apk 时,菜单图标不会出现在主布局 (activity_main.xml) 上有人知道我错过了什么吗?

menu_main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<!-- NEW NOTE -->
<item
    android:id="@+id/action_create"
    android:enabled="true"
    android:icon="@android:drawable/ic_menu_add"
    android:orderInCategory="0"
    android:title="create"
    android:visible="true"
    app:showAsAction="always" />

<!-- SETTINGS -->
<item
    android:id="@+id/action_settings"
    android:enabled="true"
    android:icon="@android:drawable/ic_menu_manage"
    android:orderInCategory="10"
    android:title="settings"
    android:visible="true"
    app:showAsAction="ifRoom" />

   </menu>

MainNote.java:

package com.example.lenovo.home;

  import android.content.Context;
   import android.content.Intent;
  import android.os.Bundle;
  import android.support.annotation.IdRes; 
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.CardView;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.ListView;
  import android.widget.Toast;
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.Comparator;
  import android.view.View.OnClickListener;



 public class MainNote extends AppCompatActivity {

private ListView mListNotes;

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

    mListNotes = (ListView) findViewById(R.id.main_listview);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main_activity, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case R.id.action_create: //run NoteActivity in new note mode
            startActivity(new Intent(this, NoteActivity.class));
            break;

        case R.id.action_settings:
            //TODO show settings activity
            break;
    }

    return super.onOptionsItemSelected(item);
}




@Override
protected void onResume() {
    super.onResume();

    //load saved notes into the listview
    //first, reset the listview
    mListNotes.setAdapter(null);
    ArrayList<Note> notes = Utilities.getAllSavedNotes(getApplicationContext());

    //sort notes from new to old
    Collections.sort(notes, new Comparator<Note>() {
        @Override
        public int compare(Note lhs, Note rhs) {
            if (lhs.getDateTime() > rhs.getDateTime()) {
                return -1;
            } else {
                return 1;
            }
        }
    });

    if (notes != null && notes.size() > 0) { //check if we have any notes!
        final NoteAdapter na = new NoteAdapter(this, R.layout.view_note_item, notes);
        mListNotes.setAdapter(na);

        //set click listener for items in the list, by clicking each item the note should be loaded into NoteActivity
        mListNotes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //run the NoteActivity in view/edit mode
                String fileName = ((Note) mListNotes.getItemAtPosition(position)).getDateTime()
                        + Utilities.FILE_EXTENSION;
                Intent viewNoteIntent = new Intent(getApplicationContext(), NoteActivity.class);
                viewNoteIntent.putExtra(Utilities.EXTRAS_NOTE_FILENAME, fileName);
                startActivity(viewNoteIntent);
            }
        });
    } else { //remind user that we have no notes!
        Toast.makeText(getApplicationContext(), "you have no saved notes!\ncreate some new notes :)"
                , Toast.LENGTH_SHORT).show();
    }
    }
     }

而且,最后是我的activity_main_note.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.lenovo.home.MainNote">

<ListView
    android:id="@+id/main_listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="visible" />

 </LinearLayout>

更新

这是我的样式代码

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>

这是我的应用程序主题代码。

标签: javaandroid

解决方案


我刚刚运行了你的代码,它工作正常。

在此处输入图像描述

我在styles.xml 中使用以下主题运行它:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

在清单中我有android:theme="@style/AppTheme"标签application

我的Activity延伸AppCompatActivity

还要检查您是否已将菜单 xml 放入res/menu文件夹中。

我唯一看到的但与所选择的项目有关的是返回语句是错误的,但这与未出现的图标无关。

如果onOptionsItemSelected(MenuItem item)处理请求,则应返回 true,否则返回 false。

像这样改变它:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    boolean rtn = false;
    switch (item.getItemId()) {
        case R.id.action_create: //run NoteActivity in new note mode
            startActivity(new Intent(this, NoteActivity.class));
            rtn = true;
            break;

        case R.id.action_settings:
            //TODO show settings activity
            rtn = true;
            break;
        default:
            rtn = super.onOptionsItemSelected(item);
    }

    return rtn;
}

看到这个问题:“android:onOptionsItemSelected”应该返回真还是假

编辑我

我在您正在扩展的 styles.xml 中看到Theme.AppCompat.Light.NoActionBar,它没有 ActionBar。

在这种情况下,您可以使用我正在使用的 ActionBar 切换到主题,否则您需要使用 Toolbar 将自己的操作栏添加到布局中


推荐阅读