天天看點

安卓菜單應用一、菜單概述二、選項菜單案例示範三、上下文菜單案例示範四、子菜單案例示範五、利用菜單配置檔案生成菜單

文章目錄

  • 一、菜單概述
  • 二、選項菜單案例示範
    • 1、實作步驟
      • (1)、建立安卓應用【OptionsMenuDemo】
      • (2)、準備圖檔素材
      • (3)、字元串資源檔案
      • (4)、主布局資源檔案
      • (5)、主界面類實作功能
  • 三、上下文菜單案例示範
    • 1、實作步驟
      • (1)、建立安卓應用【ContextMenuDemo】
      • (2)、準備圖檔素材
      • (3)、字元串資源檔案strings.xml
      • (4)、主布局資源檔案
      • (5)、主界面類實作功能
    • 2、 檢視運作結果
  • 四、子菜單案例示範
    • 1、實作步驟
      • (1)、建立安卓應用
      • (2)、将背景圖檔素材
      • (3)、字元串資源檔案strings.xml
      • (4)、主布局資源檔案
      • (5)、主界面類實作功能
    • 2、檢視效果
  • 五、利用菜單配置檔案生成菜單
    • 1、實作步驟
      • (1)、建立安卓應用
      • (2)、将圖檔素材拷貝到drawable目錄
      • (3)、字元串資源檔案
      • (4)、主布局資源檔案
      • (5)、單配置檔案main.xml
      • (6)、主界面類實作功能
    • 2、檢視效果

一、菜單概述

  • 本次課我們準備講解菜單,菜單在安卓應用程式中占有比較重要的位置。原生安卓提供了三種類型的菜單:選項菜單(OptionsMenu)、上下文菜單(ContextMenu)和子菜單(SubMenu)。在實際安卓項目中,往往會使用SlidingMenu(滑動菜單),但是要求我們學習如何使用第三方開源庫。

二、選項菜單案例示範

  • 選項菜單針對整個應用程式,提供的是全局性的功能選項。

1、實作步驟

(1)、建立安卓應用【OptionsMenuDemo】

安卓菜單應用一、菜單概述二、選項菜單案例示範三、上下文菜單案例示範四、子菜單案例示範五、利用菜單配置檔案生成菜單
  • 單擊【finish】按鈕

(2)、準備圖檔素材

安卓菜單應用一、菜單概述二、選項菜單案例示範三、上下文菜單案例示範四、子菜單案例示範五、利用菜單配置檔案生成菜單

(3)、字元串資源檔案

  • 字元串資源檔案 -

    string.xml

    安卓菜單應用一、菜單概述二、選項菜單案例示範三、上下文菜單案例示範四、子菜單案例示範五、利用菜單配置檔案生成菜單
<resources>
    <string name="app_name">選項菜單示範</string>
    <string name="popup_options_menu">彈出選項菜單</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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    tools:context=".MainActivity">

</LinearLayout>
           

(5)、主界面類實作功能

  • 主界面類-

    MainActivty

    安卓菜單應用一、菜單概述二、選項菜單案例示範三、上下文菜單案例示範四、子菜單案例示範五、利用菜單配置檔案生成菜單
  • 聲明菜單辨別常量
    安卓菜單應用一、菜單概述二、選項菜單案例示範三、上下文菜單案例示範四、子菜單案例示範五、利用菜單配置檔案生成菜單
  • 建立菜單項選擇事件方法
package net.xyx.optionsmenudemo;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private static final int NEW_FILE_MENU = 1;//建立檔案菜單辨別
    private static final int OPEN_FINE_MENU = 2;//打開檔案菜單辨別
    private static final int SAVE_FINE_MENU = 3;//儲存檔案菜單辨別
    private static final int EXIT_APP_MENU = 4;//退出應用菜單辨別

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    /**
     *
     * @param menu
     * @return 是否成功
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        //添加四個菜單項(組辨別、菜單項辨別、菜單項序号、菜單項标題)
        menu.add(0,NEW_FILE_MENU,1, "建立檔案");
        menu.add(0,OPEN_FINE_MENU,1, "打開檔案");
        menu.add(0,SAVE_FINE_MENU,1, "儲存檔案");
        menu.add(0,EXIT_APP_MENU,1, "退出應用");
        return true;
    }

    /**
     * 菜單項選擇事件處理方法
     *
     * @param item
     * @return 是否成功
     */
    @Override
    public boolean onOptionsItemSelected(@Nullable MenuItem item){
        //根據菜單項辨別判斷使用者單擊了哪一個菜單項
        switch (item.getItemId()){
            case NEW_FILE_MENU://建立檔案菜單項
                Toast.makeText(this,"你單擊了【建立檔案】菜單項~",Toast.LENGTH_SHORT).show();
                break;
            case OPEN_FINE_MENU://打開檔案菜單項
                Toast.makeText(this,"你單擊了【打開檔案】菜單項~",Toast.LENGTH_SHORT).show();
                break;
            case SAVE_FINE_MENU://儲存檔案菜單項
                Toast.makeText(this,"你單擊了【儲存檔案】菜單項~",Toast.LENGTH_SHORT).show();
                break;
            case EXIT_APP_MENU://退出應用菜單項
                Toast.makeText(this,"你單擊了【退出應用】菜單項~",Toast.LENGTH_SHORT).show();
                break;
        }
        return true;
    }
}
           

三、上下文菜單案例示範

  • 對不同控件注冊不同的上下文菜單,長按控件彈出屬于該控件的上下文菜單。

1、實作步驟

(1)、建立安卓應用【ContextMenuDemo】

安卓菜單應用一、菜單概述二、選項菜單案例示範三、上下文菜單案例示範四、子菜單案例示範五、利用菜單配置檔案生成菜單

(2)、準備圖檔素材

安卓菜單應用一、菜單概述二、選項菜單案例示範三、上下文菜單案例示範四、子菜單案例示範五、利用菜單配置檔案生成菜單

(3)、字元串資源檔案strings.xml

安卓菜單應用一、菜單概述二、選項菜單案例示範三、上下文菜單案例示範四、子菜單案例示範五、利用菜單配置檔案生成菜單
<resources>
    <string name="app_name">上下文菜單示範</string>
    <string name="file">檔案</string>
    <string name="edit">編輯</string>
</resources>
           

(4)、主布局資源檔案

  • 主布局資源檔案 -

    activity_main.xml

    安卓菜單應用一、菜單概述二、選項菜單案例示範三、上下文菜單案例示範四、子菜單案例示範五、利用菜單配置檔案生成菜單
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    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)、主界面類實作功能

  • 主界面類 -

    MainActivity

    安卓菜單應用一、菜單概述二、選項菜單案例示範三、上下文菜單案例示範四、子菜單案例示範五、利用菜單配置檔案生成菜單
  • 聲明變量和常量
    安卓菜單應用一、菜單概述二、選項菜單案例示範三、上下文菜單案例示範四、子菜單案例示範五、利用菜單配置檔案生成菜單
  • 擷取控件執行個體
    安卓菜單應用一、菜單概述二、選項菜單案例示範三、上下文菜單案例示範四、子菜單案例示範五、利用菜單配置檔案生成菜單
  • 給兩個标簽控件注冊上下文菜單
    安卓菜單應用一、菜單概述二、選項菜單案例示範三、上下文菜單案例示範四、子菜單案例示範五、利用菜單配置檔案生成菜單
  • 編輯設定圖示可用方法和上下文菜單
    安卓菜單應用一、菜單概述二、選項菜單案例示範三、上下文菜單案例示範四、子菜單案例示範五、利用菜單配置檔案生成菜單
  • 編寫上下文菜單項選擇事件處理方法
    安卓菜單應用一、菜單概述二、選項菜單案例示範三、上下文菜單案例示範四、子菜單案例示範五、利用菜單配置檔案生成菜單
  • 檢視完整代碼
package net.xyx.contextmenu;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.reflect.Method;

public class MainActivity extends AppCompatActivity {

    private TextView tvFile;//檔案标簽
    private TextView tvEdit;//編輯标簽

    private static final int NEW_FILE_MENU_ITEM = 1;//建立檔案菜單項辨別
    private static final int OPEN_FILE_MENU_ITEM = 2;//打開檔案菜單項辨別
    private static final int SAVE_FILE_MENU_ITEM = 3;//儲存檔案菜單項辨別
    private static final int EXIT_APP_MENU_ITEM = 4;//退出應用菜單項辨別

    private static final int CUT_MENU_TIEM = 5;//剪切菜單項辨別
    private static final int COPY_MENU_TIEM = 6;//拷貝菜單項辨別
    private static final int PASTE_MENU_TIEM = 7;//粘貼菜單項辨別

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //利用布局資源檔案設定使用者界面
        setContentView(R.layout.activity_main);

        //通過資源辨別符擷取控件執行個體
        tvFile = findViewById(R.id.tv_file);
        tvEdit = findViewById(R.id.tv_edit);

        //給兩個标簽控件注冊上下文菜單
        registerForContextMenu(tvFile);
        registerForContextMenu(tvEdit);
    }

        /**
         * 設定圖示可用的方法
         *
         * @param menu
         * @param enabled
         */
        private void setIconEnabled(Menu menu, boolean enabled){
            try {
                Class<?> clazz = Class.forName("com.android.internal.view.menu.MenuBuilder");
                Method m = clazz.getDeclaredMethod("setOptionalIconsVisible", boolean.class);
                m.setAccessible(true);
                // MenuBuilder實作Menu接口,建立菜單時,傳進來的menu其實就是MenuBuilder對象(Java的多态特征)
                m.invoke(menu, enabled);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    /**
     * 建立上下文菜單
     *
     * @param menu
     * @param v
     * @param menuInfo
     */
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){
        super.onCreateContextMenu(menu, v, menuInfo);
        //設定菜單圖示可用
        setIconEnabled(menu, true);
        //針對不同控件建立不同的上下菜單
        switch (v.getId()){
            case R.id.tv_file://檔案标簽
                //設定标題圖示
                menu.setHeaderIcon(R.drawable.file);
                //設定菜單标題
                menu.setHeaderTitle(R.string.file);
                //添加菜單項(組辨別、菜單項辨別、菜單項序号、菜單項标題)
                menu.add( 1,NEW_FILE_MENU_ITEM, 1, "建立檔案").setIcon(R.drawable.new_file);
                menu.add( 1,OPEN_FILE_MENU_ITEM, 2, "打開檔案").setIcon(R.drawable.open_file);
                menu.add( 1,SAVE_FILE_MENU_ITEM, 3, "儲存檔案").setIcon(R.drawable.save_file);
                menu.add( 1,EXIT_APP_MENU_ITEM, 4, "退出應用").setIcon(R.drawable.exit_app);
                break;
            case R.id.tv_edit://編輯标簽
                //設定菜單圖示
                menu.setHeaderIcon(R.drawable.edit);
                //設定菜單标題
                menu.setHeaderTitle(R.string.edit);
                //添加菜單項(組辨別、菜單項辨別、菜單項序号、菜單項标題)
                menu.add( 2,CUT_MENU_TIEM, 1, "剪切").setIcon(R.drawable.cut);
                menu.add( 2,COPY_MENU_TIEM, 2, "複制").setIcon(R.drawable.copy);
                menu.add( 2,PASTE_MENU_TIEM, 3, "粘貼").setIcon(R.drawable.paste);
                break;
        }
    }

    /**
     * 上下文菜單項選擇事件處理方法
     * @param item
     * @return
     */
    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        //根據菜單項辨別判斷使用者選擇了哪一個菜單項
        switch(item.getItemId()){
            case NEW_FILE_MENU_ITEM://建立檔案菜單項
                Toast.makeText(this,"你單擊了【建立檔案】菜單項!",Toast.LENGTH_SHORT).show();
                break;
            case OPEN_FILE_MENU_ITEM://打開檔案菜單項
                Toast.makeText(this,"你單擊了【打開檔案】菜單項!",Toast.LENGTH_SHORT).show();
                break;
            case SAVE_FILE_MENU_ITEM://儲存檔案菜單項
                Toast.makeText(this,"你單擊了【儲存檔案】菜單項!",Toast.LENGTH_SHORT).show();
                break;
            case EXIT_APP_MENU_ITEM://退出應用菜單項
                finish();//退出應用,退出目前視窗
                break;
            case CUT_MENU_TIEM://剪切菜單項
                Toast.makeText(this,"你單擊了【剪切】菜單項!",Toast.LENGTH_SHORT).show();
                break;
            case COPY_MENU_TIEM://複制菜單項
                Toast.makeText(this,"你單擊了【複制】菜單項!",Toast.LENGTH_SHORT).show();
                break;
            case PASTE_MENU_TIEM://粘貼菜單項
                Toast.makeText(this,"你單擊了【粘貼】菜單項!",Toast.LENGTH_SHORT).show();
                break;
        }
        return true;
    }
}
           

2、 檢視運作結果

菜單應用

四、子菜單案例示範

  • 子菜單既可以是選項菜單的子菜單,也可以是上下文菜單的子菜單。

1、實作步驟

(1)、建立安卓應用

  • 基于 Empty Activity 模闆建立安卓應用 -

    【SubMenuDemo】

    安卓菜單應用一、菜單概述二、選項菜單案例示範三、上下文菜單案例示範四、子菜單案例示範五、利用菜單配置檔案生成菜單
  • 單擊【finish】按鈕
    安卓菜單應用一、菜單概述二、選項菜單案例示範三、上下文菜單案例示範四、子菜單案例示範五、利用菜單配置檔案生成菜單

(2)、将背景圖檔素材

  • 将背景圖檔拷貝到

    drawable

    目錄
    安卓菜單應用一、菜單概述二、選項菜單案例示範三、上下文菜單案例示範四、子菜單案例示範五、利用菜單配置檔案生成菜單

(3)、字元串資源檔案strings.xml

安卓菜單應用一、菜單概述二、選項菜單案例示範三、上下文菜單案例示範四、子菜單案例示範五、利用菜單配置檔案生成菜單
<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:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root"
    android:background="@drawable/background"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    
</LinearLayout>
           

(5)、主界面類實作功能

  • 主界面類 -

    MainActivity

    安卓菜單應用一、菜單概述二、選項菜單案例示範三、上下文菜單案例示範四、子菜單案例示範五、利用菜單配置檔案生成菜單
  • 執行個體
    安卓菜單應用一、菜單概述二、選項菜單案例示範三、上下文菜單案例示範四、子菜單案例示範五、利用菜單配置檔案生成菜單
  • 通過資源辨別符擷取控件執行個體
    安卓菜單應用一、菜單概述二、選項菜單案例示範三、上下文菜單案例示範四、子菜單案例示範五、利用菜單配置檔案生成菜單
  • 建立選項菜單及其子菜單
    安卓菜單應用一、菜單概述二、選項菜單案例示範三、上下文菜單案例示範四、子菜單案例示範五、利用菜單配置檔案生成菜單
  • 編寫菜單項事件處理方法
    安卓菜單應用一、菜單概述二、選項菜單案例示範三、上下文菜單案例示範四、子菜單案例示範五、利用菜單配置檔案生成菜單

2、檢視效果

1670209146203

五、利用菜單配置檔案生成菜單

  • 利用菜單配置檔案來生成菜單十分友善,既可用于生成選項菜單,也可用于生成上下文菜單。

1、實作步驟

(1)、建立安卓應用

  • 基于 Empty Activity 模闆建立安卓應用 -【

    XMLMenu

    安卓菜單應用一、菜單概述二、選項菜單案例示範三、上下文菜單案例示範四、子菜單案例示範五、利用菜單配置檔案生成菜單
  • 單擊【finish】按鈕
    安卓菜單應用一、菜單概述二、選項菜單案例示範三、上下文菜單案例示範四、子菜單案例示範五、利用菜單配置檔案生成菜單

(2)、将圖檔素材拷貝到drawable目錄

安卓菜單應用一、菜單概述二、選項菜單案例示範三、上下文菜單案例示範四、子菜單案例示範五、利用菜單配置檔案生成菜單

(3)、字元串資源檔案

字元串資源檔案 -

string.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_app">退出程式</string>
    <string name="edit_menu">編輯</string>
    <string name="cut">剪切</string>
    <string name="copy">複制</string>
    <string name="paste">粘貼</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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    tools:context=".MainActivity">

</LinearLayout>
           

(5)、單配置檔案main.xml

  • 在res目錄裡建立menu目錄,然後在裡面建立菜單配置檔案

    main.xml

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="collapseActionView">
    <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_app_menu_item"
            android:icon="@drawable/exit"
            android:title="@string/exit_app"
            app:showAsAction="ifRoom|withText" />
    </menu>
</item>
<item
    android:id="@+id/edit_menu"
    android:title="@string/edit_menu"
    app:showAsAction="aollapseActionView">
    <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)、主界面類實作功能

  • 主界面類 -

    MainActivity

    安卓菜單應用一、菜單概述二、選項菜單案例示範三、上下文菜單案例示範四、子菜單案例示範五、利用菜單配置檔案生成菜單
  • 利用菜單配置檔案生成選項菜單
  • 利用getMenuInflator()擷取菜單填充器,調用其inflate()方法将菜單配置檔案生成選項菜單,第一個參數是菜單配置檔案辨別,第二個參數是選項菜單對象
    安卓菜單應用一、菜單概述二、選項菜單案例示範三、上下文菜單案例示範四、子菜單案例示範五、利用菜單配置檔案生成菜單
  • 編寫菜單項選擇事件處理方法
    安卓菜單應用一、菜單概述二、選項菜單案例示範三、上下文菜單案例示範四、子菜單案例示範五、利用菜單配置檔案生成菜單

2、檢視效果

菜單