零、学习目标
- 能说出安卓三种常用菜单
- 能利用菜单编写简单安卓应用
一、菜单概述
- 本次课我们准备讲解菜单,菜单在安卓应用程序中占有比较重要的位置。原生安卓提供了三种类型的菜单:选项菜单(OptionsMenu)、上下文菜单(ContextMenu)和子菜单(SubMenu)。在实际安卓项目中,往往会使用SlidingMenu(滑动菜单),但是要求我们学习如何使用第三方开源库。
二、选项菜单案例演示
- 选项菜单针对整个应用程序,提供的是全局性的功能选项。
(一)实现步骤
1、创建安卓应用【OptionsMenuDemo】
![](https://img-blog.csdnimg.cn/f809cfb3366e4b95aa4df283b03b3854.png)
2、将背景图片拷贝到drawable目录
![](https://img-blog.csdnimg.cn/5ecbc12767d146acb5d071c4aa34fd62.png)
3、字符串资源文件strings.xml
![](https://img-blog.csdnimg.cn/59b235c88a654cebac9aa9559d0c1333.png)
<resources>
<string name="app_name">选项菜单演示</string>
<string name="popup_options_menu">弹出选项菜单</string>
</resources>
4、主布局资源文件activity_main.xml
![](https://img-blog.csdnimg.cn/66b896e52ef44416bea57562de6ee390.png)
<?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>
5、主界面类 - MainActivity