4.7菜单

本文详细介绍了安卓应用程序中菜单的使用,包括选项菜单、上下文菜单和子菜单的创建及事件处理,还展示了如何利用菜单配置文件生成菜单,是安卓开发者必备的知识。
摘要由CSDN通过智能技术生成

一、菜单概述

菜单在安卓应用程序中占有比较重要的位置。原生安卓提供了三种类型的菜单:选项菜单(OptionsMenu)、上下文菜单(ContextMenu)和子菜单(SubMenu)。在实际安卓项目中,往往会使用SlidingMenu(滑动菜单),但是要求我们学习如何使用第三方开源库。

二、选项菜单案例演示

  • 选项菜单针对整个应用程序,提供的是全局性的功能选项。

1、创建安卓应用

(一)、创建安卓应用【OptionsMenuDemo】

在这里插入图片描述

(二)将背景图片拷贝到drawable目录

在这里插入图片描述

(三)字符串资源文件strings.xml

<resources>
    <string name="app_name">选项菜单演示</string>
</resources>

(四)主布局资源文件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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:gravity="center"
    tools:context=".MainActivity">

</LinearLayout>

(五)主界面类 - MainActivity

  • 声明菜单标识常量
    在这里插入图片描述
  • 创建选项菜单
    在这里插入图片描述
  • 创建菜单项选择事件方法
    在这里插入图片描述

(六)演示结果

在这里插入图片描述

三、上下文菜单案例演示

  • 对不同控件注册不同的上下文菜单,长按控件弹出属于该控件的上下文菜单。

1、创建安卓应用【ContextMenuDemo】

在这里插入图片描述

2、将图片素材拷贝到drawable目录

在这里插入图片描述

3、字符串资源文件strings.xml

<resources>
    <string name="app_name">上下文菜单演示</string>
    <string name="file">文件</string>
    <string name="edit">编辑</string>
</resources>

4、主布局资源文件acitivity_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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp"
        android:background="#eeeeee">

        <TextView
            android:id="@+id/tv_file"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="15dp"
            android:text="@string/file"
            android:textColor="#0000ff"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/tv_edit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/edit"
            android:textColor="#0000ff"
            android:textSize="20sp" />
    </LinearLayout>
</LinearLayout>

5、查看预览效果

在这里插入图片描述

5、主界面类 - MainActivity

  • 声明变量与常量
    在这里插入图片描述

  • 通过资源标识符获取控件实例
    在这里插入图片描述

  • 给控件注册上下文菜单
    在这里插入图片描述

  • 编写设置图标可用的方法
    在这里插入图片描述

  • 编写创建上下文菜单的方法
    在这里插入图片描述

  • 编写上下文菜单项选择事件处理方法
    在这里插入图片描述

6、运行结果

在这里插入图片描述

7、源代码

package net.ls.contextmenudemo;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值