Common controls for Android notes 5.6 - Menu

Zero, learning objectives

  • Can name three commonly used menus in Android
  • Ability to write simple Android applications using menus

1. Menu Overview

  • In this lesson we are going to explain the menu, which occupies a relatively important position in Android applications. Native Android provides three types of menus: options menu (OptionsMenu), context menu (ContextMenu) and submenu (SubMenu). In actual Android projects, SlidingMenu (sliding menu) is often used, but it requires us to learn how to use third-party open source libraries.

2. Option menu case demonstration

  • The options menu provides global functional options for the entire application.

(1) Implementation steps

1. Create Android application [OptionsMenuDemo]

2. Copy the background image to the drawable directory 

3, StringResource file strings.xml 

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

4. Main layout resource file 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">

    <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. Main interface class - MainActivity

  • Declare menu identification constants 
  •  Create options menu
  • Create menu item selection event method 
  •  Write a pop-up option menu button click event handling method

Run to see the effect 

3. Context menu case demonstration

1. Create a new project

 

2. Prepare picture materials 

3. String resource file strings.xml 

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

 4. Main layout resource file activity_main.xml

5. Main interface class - MainActivity 

Implementation functions of the main interface class

  • Declare variables and constants
  • Get the control instance by resource identifier
    
  • Register a context menu for a control 
  • Write methods that set the icon available
  •  
  • Write a method to create a context menu
  •  
  • Write context menu item selection event handling method
  •  

 6. Start the application and check the effect

Changan [File] or [Edit] tab, the context menu pops up

4. Submenu case demonstration

1. Create an Android application

 

2. Copy the background image to the drawable directory 

3. String resource file strings.xml 

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

 4. Main layout resource file 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>

 5. Main interface class - MainActivity

 

  • Declare variables and constants 
  •  Get the control instance through the resource identifier
  • Write menu item event handling method 

 

 

 Write menu item event handling method

6. Start the application and check the effect 

5. Generate menu using menu configuration file

1. Create an Android application

 

2. Prepare materials

3. String resource file strings.xml 

<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. Main layout resource file activity_main.xml

5. Menu configuration file main.xml 

  • Create the menu directory in the res directory, and then create the menu configuration file main.xml in it
  • <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>
    

    6. Main interface class - MainActivity

  • Generate options menu using menu configuration file
  • Use getMenuInflator() to obtain the menu inflator, and call its inflate() method to generate an options menu from the menu configuration file. The first parameter is the menu configuration file identifier, and the second parameter is the options menu object.
  •  Write menu item event handling method

 7. Start the application and check the effect

 

 

Guess you like

Origin blog.csdn.net/justin02191004/article/details/128085700