Android应用菜单图标不显示问题排查求助
Android笔记应用菜单图标不显示问题排查
我按照教程开发Android笔记应用,但运行APK时,主布局(activity_main_note.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>
更新补充:styles.xml
<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="windowNoTi
嘿,我一眼就瞅出问题所在啦!你的菜单图标不显示核心原因是当前主题没有ActionBar!
看你styles.xml里的AppTheme,父主题是Theme.AppCompat.Light.NoActionBar,而且还额外设置了windowActionBar=false——这相当于直接把承载菜单的ActionBar给关掉了,菜单图标自然没地方落脚。给你两个简单的解决办法:
方法一:切换带ActionBar的主题
直接修改styles.xml里的AppTheme父主题,换成带ActionBar的版本,比如Theme.AppCompat.Light.DarkActionBar,这样系统会自动给你生成ActionBar,菜单图标就能正常显示了。修改后的代码大概是这样:
<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>
方法二:手动添加Toolbar替代ActionBar
如果你就是想用无ActionBar的主题,那可以手动在布局里加个Toolbar,然后把它设置成ActionBar来用:
- 先修改activity_main_note.xml,添加上Toolbar:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" 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"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay"/> <ListView android:id="@+id/main_listview" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="visible" /> </LinearLayout>
- 再在MainNote.java的
onCreate方法里添加两行代码,把Toolbar设置为ActionBar:
Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar);
另外提一句,你styles.xml里的AppTheme.NoActionBar代码没写完,记得补全,但这不是当前问题的核心,先解决ActionBar的问题就行~
内容的提问来源于stack exchange,提问作者user9815533
相关产品推荐
相关产品推荐

