我的应用主屏幕上有几个按钮和列表视图。我想把菜单活动放在它上面。所以我刚刚编写了自定义菜单活动并从主屏幕扩展。但不幸的是,当我点击手机上的菜单按钮时,我的自定义菜单无效。有什么想法吗?
package com.gokiri.gl.activity;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import com.gokiri.gl.R;
import com.gokiri.gl.screen.GLandMainMenu;
import com.gokiri.gl.screen.SearchScreen;
import com.gokiri.gl.util.Shared;
public class GLandMainActivity extends ListActivity {
protected Activity act;
protected int screenWidth, screenHeight;
protected Bitmap bannerBitmap;
protected void initActivity(Activity act) {
this.act = act;
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels;
dm = null;
Shared.addAcitivity(act);
setDefaultButton();
}
public void setDefaultButton() {
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
Shared.removeAcitivity();
startActivity(new Intent(this, GLandMainMenu.class));
return true;
}
return super.onKeyDown(keyCode, event);
}
private Menu mMenu;
public boolean onCreateOptionsMenu(Menu menu) {
this.mMenu = menu;
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
public boolean onPrepareOptionsMenu(Menu menu) {
return super.onPrepareOptionsMenu(menu);
}
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_home:
// act.finish();
Intent main = new Intent(act, GLandMainMenu.class);
startActivity(main);
return true;
case R.id.menu_search:
act.finish();
Intent search = new Intent(act, SearchScreen.class);
startActivity(search);
return true;
case R.id.menu_bookmarks:
return true;
case R.id.menu_about:
return true;
}
return super.onMenuItemSelected(featureId, item);
}
}
评论编辑:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_home"
android:title="@string/home"
android:icon="@drawable/tab_home" />
<item android:id="@+id/menu_search"
android:title="@string/search"
android:icon="@drawable/tab_search" />
<item android:id="@+id/menu_bookmarks"
android:title="@string/bookmark"
android:icon="@drawable/tab_about" />
<item android:id="@+id/menu_about"
android:title="@string/about"
android:icon="@drawable/tab_about" />
</menu>
答案 0 :(得分:2)
对于我所看到的,你使用截取KeyEvent.KEYCODE_MENU的onKeyDown来阻止onCreateOptionMenu()。您无法使用菜单按钮进行启动活动和显示菜单(显然)。如果不是这样,请解释一下GLandMainMenu类是什么