标签:
package cn.fansunion.menu; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.widget.Toast; import cn.fansunion.R; public class OptionMenuWithXmlActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.option_menu); } public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu, menu); return true; } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_one: Toast.makeText(this, "你点击了menu_one", Toast.LENGTH_LONG).show(); break; case R.id.submenu_one: Toast.makeText(this, "你点击了submenu_one", Toast.LENGTH_LONG).show(); break; default: return super.onOptionsItemSelected(item); } return true; } }
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/menu_one" android:orderInCategory="3" android:title="Android应用开发真有趣"/> <item android:id="@+id/menu_two" android:orderInCategory="2" android:title="iOS开发也不错"/> <item android:id="@+id/menu_three" android:enabled="false" android:orderInCategory="4" android:title="Java服务端也不错"/> <!-- Part 2:子menu的设置,将在menuItem内部嵌套一个<Menu>,在这个例子中的子菜单,试验了快捷键的方式 --> <item android:id="@+id/submenu" android:orderInCategory="3" android:title="小雷的兴趣"> <menu > <item android:id="@+id/submenu_one" android:alphabeticShortcut="n" android:title="互联网技术" android:visible="true"/> <item android:id="@+id/submenu_two" android:alphabeticShortcut="g" android:title="投资理财" android:visible="true"/> </menu></item> </menu>
标签:
原文地址:http://blog.csdn.net/fansunion/article/details/49947695