我试图获取我的选项菜单图标,该图标通常出现在平板电脑屏幕的一角。它不在那里!我目前正在使用Android 4.2.2进行开发。这是我正在使用的当前Java代码:
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
boolean result = super.onCreateOptionsMenu(menu);
//theMenu = menu;
//new MenuInflater(getApplication()).inflate(R.menu.loggermap, menu);
menu.add(ContextMenu.NONE, MENU_TRACKING, ContextMenu.NONE, R.string.menu_tracking).setIcon(R.drawable.ic_menu_movie).setAlphabeticShortcut('T');
menu.add(ContextMenu.NONE, MENU_LAYERS, ContextMenu.NONE, R.string.menu_showLayers).setIcon(R.drawable.ic_menu_mapmode).setAlphabeticShortcut('L');
menu.add(ContextMenu.NONE, MENU_NOTE, ContextMenu.NONE, R.string.menu_insertnote).setIcon(R.drawable.ic_menu_myplaces);
menu.add(ContextMenu.NONE, MENU_STATS, ContextMenu.NONE, R.string.menu_statistics).setIcon(R.drawable.ic_menu_picture).setAlphabeticShortcut('S');
menu.add(ContextMenu.NONE, MENU_SHARE, ContextMenu.NONE, R.string.menu_shareTrack).setIcon(R.drawable.ic_menu_share).setAlphabeticShortcut('I');
menu.add(ContextMenu.NONE, MENU_TRACKLIST, ContextMenu.NONE, R.string.menu_tracklist).setIcon(R.drawable.ic_menu_show_list).setAlphabeticShortcut('P');
menu.add(ContextMenu.NONE, MENU_SETTINGS, ContextMenu.NONE, R.string.menu_settings).setIcon(R.drawable.ic_menu_preferences).setAlphabeticShortcut('C');
MenuItem noteMenu = menu.findItem(MENU_NOTE);
noteMenu.setEnabled(true);
return result;
//return (super.onCreateOptionsMenu(menu));
}
/*
* (non-Javadoc)
* @see android.app.Activity#onPrepareOptionsMenu(android.view.Menu)
*/
@Override
public boolean onPrepareOptionsMenu(Menu menu)
{
MenuItem noteMenu = menu.findItem(MENU_NOTE);
noteMenu.setVisible(true);
//noteMenu.setEnabled(mLoggerServiceManager.isMediaPrepared());
noteMenu.setEnabled(true);
MenuItem shareMenu = menu.findItem(MENU_SHARE);
shareMenu.setVisible(true);
//shareMenu.setEnabled(mTrackId >= 0);
shareMenu.setEnabled(true);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
boolean handled = false;
Uri trackUri;
Intent intent;
switch (item.getItemId())
{
case MENU_TRACKING:
intent = new Intent(this, ControlTracking.class);
startActivityForResult(intent, MENU_TRACKING);
handled = true;
break;
case MENU_LAYERS:
showDialog(DIALOG_LAYERS);
handled = true;
break;
case MENU_NOTE:
/*intent = new Intent(this, InsertNote.class);
startActivityForResult(intent, MENU_NOTE);*/
intent = new Intent(this, WaypointNewActivity.class);
startActivity(intent);
handled = true;
break;
case MENU_SETTINGS:
intent = new Intent(this, ApplicationPreferenceActivity.class);
startActivity(intent);
handled = true;
break;
case MENU_TRACKLIST:
intent = new Intent(this, TrackList.class);
intent.putExtra(Tracks._ID, this.mTrackId);
startActivityForResult(intent, MENU_TRACKLIST);
break;
case MENU_STATS:
if (this.mTrackId >= 0)
{
intent = new Intent(this, Statistics.class);
trackUri = ContentUris.withAppendedId(Tracks.CONTENT_URI, mTrackId);
intent.setData(trackUri);
startActivity(intent);
handled = true;
break;
}
else
{
showDialog(DIALOG_NOTRACK);
}
handled = true;
break;
case MENU_ABOUT:
intent = new Intent("org.openintents.action.SHOW_ABOUT_DIALOG");
try
{
startActivityForResult(intent, MENU_ABOUT);
}
catch (ActivityNotFoundException e)
{
showDialog(DIALOG_INSTALL_ABOUT);
}
break;
case MENU_SHARE:
intent = new Intent(Intent.ACTION_RUN);
trackUri = ContentUris.withAppendedId(Tracks.CONTENT_URI, mTrackId);
intent.setDataAndType(trackUri, Tracks.CONTENT_ITEM_TYPE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Bitmap bm = findViewById(R.id.mapScreen).getDrawingCache();
if (bm != null)
{
Uri screenStreamUri = ShareTrack.storeScreenBitmap(bm);
intent.putExtra(Intent.EXTRA_STREAM, screenStreamUri);
}
startActivityForResult(Intent.createChooser( intent, getString( R.string.share_track ) ), MENU_SHARE);
handled = true;
break;
case MENU_CONTRIB:
showDialog(DIALOG_CONTRIB);
default:
handled = super.onOptionsItemSelected(item);
break;
}
return handled;
}
XML代码在这里:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_loggermap_tracking"
android:alphabeticShortcut="t"
android:icon="@drawable/ic_menu_movie"
android:showAsAction="ifRoom"
android:title="@string/menu_tracking"/>
<item
android:id="@+id/menu_loggermap_layers"
android:alphabeticShortcut="l"
android:icon="@drawable/ic_menu_mapmode"
android:showAsAction="ifRoom"
android:title="@string/menu_showLayers"/>
<item
android:id="@+id/menu_loggermap_note"
android:alphabeticShortcut="n"
android:icon="@drawable/ic_menu_myplaces"
android:showAsAction="ifRoom"
android:title="@string/menu_insertnote"/>
<item
android:id="@+id/menu_loggermap_stats"
android:alphabeticShortcut="s"
android:icon="@drawable/ic_menu_picture"
android:showAsAction="ifRoom"
android:title="@string/menu_statistics"/>
<item
android:id="@+id/menu_loggermap_share"
android:alphabeticShortcut="i"
android:icon="@drawable/ic_menu_share"
android:showAsAction="ifRoom"
android:title="@string/menu_shareTrack"/>
<item
android:id="@+id/menu_loggermap_tracklist"
android:alphabeticShortcut="p"
android:icon="@drawable/ic_menu_show_list"
android:showAsAction="collapseActionView"
android:title="@string/menu_tracklist"/>
<item
android:id="@+id/menu_loggermap_settings"
android:alphabeticShortcut="c"
android:icon="@drawable/ic_menu_preferences"
android:showAsAction="collapseActionView"
android:title="@string/menu_settings"/>
<item
android:id="@+id/menu_loggermap_about"
android:alphabeticShortcut="a"
android:icon="@drawable/ic_menu_info_details"
android:showAsAction="collapseActionView"
android:title="@string/menu_about"/>
<item
android:id="@+id/menu_loggermap_contrib"
android:alphabeticShortcut="o"
android:icon="@drawable/ic_menu_allfriends"
android:showAsAction="collapseActionView"
android:title="@string/menu_contrib"/>
</menu>
我认为我的Java代码有问题。我在这做错了什么?任何帮助将不胜感激!
答案 0 :(得分:0)
您没有指定正在使用的平板电脑。如果平板电脑有硬件菜单按钮 - 那些不适合的项目将在那里。
我不明白为什么你在代码中创建菜单而不是只是膨胀
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.loggermap, menu);
return true;
}
对于那些有ActionView的项目,也要添加ifRoom
android:showAsAction="ifRoom|collapseActionView"