Android provides three types of application menus:
1.Options Menu(presses the device MENU key)
最多可以显示6个按钮,不支持复选或单选按钮
如果需要有多余6个需要显示,可是用Expanded Menu进行显示
2.Context Menu
当用户长按下某个View的时候,比如:EditText
3.Submenu
A submenu item cannot support a nested submenu
给个简单例子:
1. MenuTest.java
2.game_menu.xml
1.Options Menu(presses the device MENU key)
最多可以显示6个按钮,不支持复选或单选按钮
如果需要有多余6个需要显示,可是用Expanded Menu进行显示
2.Context Menu
当用户长按下某个View的时候,比如:EditText
3.Submenu
A submenu item cannot support a nested submenu
给个简单例子:
1. MenuTest.java
package v.m;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class MenuTest extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle item selection
switch (item.getItemId())
{
case R.id.new_game:
newGame();
// it returns "true" to indicate that the item selection was handled
return true;
case R.id.quit:
quit();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void newGame()
{
System.out.println("new Game!!!");
}
public void quit()
{
finish();
}
}
2.game_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/new_game" android:icon="@drawable/icon"
android:title="New Game" />
<item android:id="@+id/quit" android:icon="@drawable/icon"
android:title="Quit" />
</menu>