【Android】菜单功能的实现:使用XML
2013-11-08 12:30
399 查看
1.编辑Main对应菜单文件夹的xml文件:
2.Main里面最重要还是那两个函数:
3.再创建一个Activity,就完成了。
注意这一句:
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/action_settings" android:icon="@drawable/ic_launcher" android:orderInCategory="100" android:showAsAction="ifRoom|withText" android:title="@string/action_settings"> <menu> <item android:id="@+id/create_new" android:title="新建文件"> </item> <item android:id="@+id/open" android:title="打开文件"> </item> </menu> </item> <item android:id="@+id/sys" android:icon="@drawable/ic_launcher" android:orderInCategory="101" android:showAsAction="never" android:title="用户菜单"> <menu> <group android:id="@+id/group1" > <item android:id="@+id/load" android:title="加载文件"> </item> </group> <group android:id="@+id/group2" > <item android:id="@+id/save" android:title="保存文件"> </item> </group> </menu> </item> </menu>
2.Main里面最重要还是那两个函数:
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case R.id.create_new: Intent intent = new Intent(MainActivity.this, SystemMunu.class); item.setIntent(intent); break; case R.id.open: Toast.makeText(MainActivity.this, "打开菜单", Toast.LENGTH_SHORT).show(); break; case R.id.load: Toast.makeText(MainActivity.this, "加载菜单", Toast.LENGTH_SHORT).show(); break; case R.id.save: Toast.makeText(MainActivity.this, "保存菜单", Toast.LENGTH_SHORT).show(); break; default: break; } return super.onMenuItemSelected(featureId, item); }
3.再创建一个Activity,就完成了。
注意这一句:
android:showAsAction="ifRoom|withText"
相关文章推荐
- Android开发——使用高级的RecyclerView实现侧滑菜单删除功能(SwipeRecyclerView)
- 【Android】菜单功能的实现:使用Java代码
- Android实现打电话的功能-使用Intent和AndroidManifset.xml中加入权限
- Android开发之使用DrawerLayout实现侧拉菜单功能
- Android 使用FragmentTabHost实现底部菜单功能
- Android实现打电话的功能-使用Intent和AndroidManifset.xml中加入权限
- Android使用开源组件PagerBottomTabStrip实现底部菜单和顶部导航功能
- 初学Android实现打电话的功能-使用Intent和AndroidManifset.xml中加入权限
- 初学Android实现打电话的功能-使用Intent和AndroidManifset.xml中加入权限
- Android使用Popupwindow实现悬浮菜单功能
- Android实现打电话的功能-使用Intent和AndroidManifset.xml中加入权限
- android之使用ListView实现多菜单功能
- Android实现打电话的功能-使用Intent和AndroidManifset.xml中加入权限
- Android 使用加速度传感器实现摇一摇功能及优化
- Ajax 和 XML: 使用 Ajax 实现打分评级和添加评论功能
- Android使用GridView实现日历功能(详细代码)
- Android编程使用Service实现Notification定时发送功能示例
- Android Material Design系列之使用Toolbar + DrawerLayout实现高侧滑菜单
- Android使用DrawerLayout实现仿QQ双向侧滑菜单
- 使用AndroidAnnotations框架优雅地实现ListView功能例子