赞
踩
- <resources>
- <string name="app_name">选项菜单演示</string>
- <string name="popup_options_menu">弹出选项菜单</string>
- </resources>
- <?xml version="1.0" encoding="utf-8"?>
- <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:background="@drawable/background"
- android:gravity="center"
- tools:context=".MainActivity">
-
- <Button
- android:id="@+id/btnPopupOptionsMenu"
- android:layout_width="200dp"
- android:layout_height="wrap_content"
- android:onClick="doPopupOptionsMenu"
- android:text="@string/popup_options_menu" />
- </LinearLayout>
运行查看效果
- <resources>
- <string name="app_name">上下文菜单演示</string>
- <string name="file">文件</string>
- <string name="edit">编辑</string>
- </resources>
主界面类的实现功能
通过资源标识符获取控件实例
长安【文件】或【编辑】标签,弹出上下文菜单
- <resources>
- <string name="app_name">子菜单演示</string>
- </resources>
4、主布局资源文件activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/root"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@drawable/background"
- android:gravity="center"
- android:orientation="vertical"
- tools:context=".MainActivity">
-
- </LinearLayout>
编写菜单项事件处理方法
- <resources>
- <string name="app_name">利用XML配置生成菜单</string>
- <string name="file_menu">文件</string>
- <string name="new_file">新建文件</string>
- <string name="open_file">打开文件</string>
- <string name="save_file">保存文件</string>
- <string name="exit">退出程序</string>
- <string name="edit_menu">编辑</string>
- <string name="cut">剪切</string>
- <string name="copy">复制</string>
- <string name="paste">粘贴</string>
- </resources>
4、主布局资源文件activity_main.xml
- <menu xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- tools:context="net.hw.xml_menu.MainActivity">
-
- <item
- android:id="@+id/file_menu"
- android:title="@string/file_menu"
- app:showAsAction="always">
- <menu>
- <item
- android:id="@+id/new_file_menu_item"
- android:icon="@drawable/new_file"
- android:title="@string/new_file"
- app:showAsAction="ifRoom|withText" />
- <item
- android:id="@+id/open_file_menu_item"
- android:icon="@drawable/open_file"
- android:title="@string/open_file"
- app:showAsAction="ifRoom|withText" />
- <item
- android:id="@+id/save_file_menu_item"
- android:icon="@drawable/save_file"
- android:title="@string/save_file"
- app:showAsAction="ifRoom|withText" />
- <item
- android:id="@+id/exit_menu_item"
- android:icon="@drawable/exit"
- android:title="@string/exit"
- app:showAsAction="ifRoom|withText" />
- </menu>
- </item>
- <item
- android:id="@+id/edit_menu"
- android:title="@string/edit_menu"
- app:showAsAction="always">
- <menu>
- <item
- android:id="@+id/cut_menu_item"
- android:icon="@drawable/cut"
- android:title="@string/cut"
- app:showAsAction="ifRoom|withText" />
- <item
- android:id="@+id/copy_menu_item"
- android:icon="@drawable/copy"
- android:title="@string/copy"
- app:showAsAction="ifRoom|withText" />
- <item
- android:id="@+id/paste_menu_item"
- android:icon="@drawable/paste"
- android:title="@string/paste"
- app:showAsAction="ifRoom|withText" />
- </menu>
- </item>
- </menu>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。