我正在尝试创建一个包含两个项目'help'和'about'的菜单。我写了代码。它没有错误,但代码不起作用。我还在清单文件中添加了活动。这是我的AndroidMenusActivity代码:
package com.easy.convert;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class AndroidMenusActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// Initiating Menu XML file (menu.xml)
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
/**
* Event Handling for Individual menu item selected
* Identify single menu item by it's id
* */
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_help:
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.aboutus);
dialog.setTitle("Help");
dialog.setCancelable(true);
dialog.show();
return true;
case R.id.menu_about:
Dialog dialog1 = new Dialog(this);
dialog1.setContentView(R.layout.aboutus);
dialog1.setTitle("About");
dialog1.setCancelable(true);
dialog1.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
menu.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/lib/com.easy.convert ">
<!-- Single menu item
Set id, icon and Title for each menu item
-->
<item android:id="@+id/menu_help"
android:icon="@drawable/help"
android:title="Help" />
<item android:id="@+id/menu_about"
android:icon="@drawable/about"
android:title="About" />
</menu>
我希望每个活动都有菜单。
答案 0 :(得分:0)
你的问题在这里:
// Initiating Menu XML file (menu.xml)
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
您可以在此处查看:Google Document
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
希望有所帮助!
答案 1 :(得分:0)
试试这个:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/lib/com.easy.convert ">
<!-- Single menu item
Set id, icon and Title for each menu item
-->
<item android:id="@+id/menu_help"
android:icon="@drawable/help"
android:showAsAction="ifRoom"
android:title="Help" />
<item android:id="@+id/menu_about"
android:icon="@drawable/about"
android:showAsAction="ifRoom"
android:title="About" />
</menu>
答案 2 :(得分:0)
将以下代码用于您的所有活动。
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu, menu);
return true;
}
/**
* Event Handling for Individual menu item selected
* Identify single menu item by it's id
* */
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_help:
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.aboutus);
dialog.setTitle("Help");
dialog.setCancelable(true);
dialog.show();
return true;
case R.id.menu_about:
Dialog dialog1 = new Dialog(this);
dialog1.setContentView(R.layout.aboutus);
dialog1.setTitle("About");
dialog1.setCancelable(true);
dialog1.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}