我只是关注另一个教程但结果却不同。当我试图运行我的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>
这是我申请的主题代码。
答案 0 :(得分:0)
我刚刚运行了你的代码,它运行正常。
我在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;
}
请参阅此问题:Should "android: onOptionsItemSelected" return true or false
编辑我
我在你的styles.xml中看到你正在扩展Theme.AppCompat.Light.NoActionBar
,它没有ActionBar。
在这种情况下,您可以使用ActionBar切换到主题,或者您需要add your own action bar to the layout using a Toolbar。