<!-- 定义基础布局LinearLayout --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical"> <!-- 定义TextView控件 --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="请点击物理菜单键显示菜单,再次点击菜单选项显示子菜单" /> </LinearLayout>
package com.example.yanlei.yl2; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.SubMenu; import android.widget.Toast; //导入必备的包 public class MainActivity extends AppCompatActivity { //定义Menu中每个菜单选项的Id final int Menu_1 = Menu.FIRST; final int Menu_2 = Menu.FIRST + 1; final int Menu_3 = Menu.FIRST + 2; final int Menu_4 = Menu.FIRST + 3; final int Menu_5 = Menu.FIRST + 4; final int Menu_6 = Menu.FIRST + 5; final int Menu_7 = Menu.FIRST + 6; final int Menu_8 = Menu.FIRST + 7; final int Menu_9 = Menu.FIRST + 8; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 调用父类的onCreate方法 // 通过setContentView方法设置当前页面的布局文件为activity_main setContentView(R.layout.activity_main); } //创建Menu菜单的回调方法 public boolean onCreateOptionsMenu(Menu m) { //定义子菜单,然后添加到Menu中 SubMenu fileMenu = m.addSubMenu("新建"); //在菜单选项的子菜单中添加选项内容 //add方法的参数:add(分组id,itemid, 排序, 菜单文字) fileMenu.add(0, Menu_1, 0, "文件1"); fileMenu.add(0, Menu_2, 0, "文件2"); fileMenu.add(0, Menu_3, 0, "文件3"); SubMenu openMenu = m.addSubMenu("打开"); //在菜单选项的子菜单中添加选项内容 //add方法的参数:add(分组id,itemid, 排序, 菜单文字) openMenu.add(0, Menu_4, 0, "文件4"); openMenu.add(0, Menu_5, 0, "文件5"); openMenu.add(0, Menu_6, 0, "文件6"); SubMenu saveMenu = m.addSubMenu("保存"); //在菜单选项的子菜单中添加选项内容 //add方法的参数:add(分组id,itemid, 排序, 菜单文字) saveMenu.add(0, Menu_7, 0, "文件7"); saveMenu.add(0, Menu_8, 0, "文件8"); saveMenu.add(0, Menu_9, 0, "文件9"); return super.onCreateOptionsMenu(m); } //Menu菜单选项的选项选择的回调事件 public boolean onOptionsItemSelected(MenuItem item) { //参数为用户选择的菜单选项对象 //根据菜单选项的id来执行相应的功能 switch (item.getItemId()) { case 1: Toast.makeText(this, "新建文件1", Toast.LENGTH_SHORT).show(); break; case 2: Toast.makeText(this, "新建文件2", Toast.LENGTH_SHORT).show(); break; case 3: Toast.makeText(this, "新建文件3", Toast.LENGTH_SHORT).show(); break; case 4: Toast.makeText(this, "打开文件4", Toast.LENGTH_SHORT).show(); break; case 5: Toast.makeText(this, "打开文件5", Toast.LENGTH_SHORT).show(); break; case 6: Toast.makeText(this, "打开文件6", Toast.LENGTH_SHORT).show(); break; case 7: Toast.makeText(this, "保存文件7", Toast.LENGTH_SHORT).show(); break; case 8: Toast.makeText(this, "保存文件8", Toast.LENGTH_SHORT).show(); break; case 9: Toast.makeText(this, "保存文件9", Toast.LENGTH_SHORT).show(); break; } return super.onOptionsItemSelected(item); } }