Follow me Android Eight ActionBar and menu

Video lesson: https://edu.csdn.net/course/play/7621

Contents of this chapter

Section 1 Menu Overview

Section 2 Option Menu and Submenu

Section 3 Context Menu

Section 4 Use XML to define the menu

Section 5 The role of ActionBar


Objective of this chapter

Understand the characteristics of each version of the menu.

Master the method of creating an option menu.

Grasp the method of handling menu events.

Master the method of dynamically changing the menu.

Master the method of creating context menus.

Familiar with the method of extending the context menu.

Understand the role of ActionBar.

Menu in Android system

u menu usually has two situations: option menu and context menu

ØThe option menu related to Activity is called the option menu. Press the MENU button to pop up when Activity is displayed

ØThe menu related to the specific view is called the context menu, and long press the view will pop up

The options menu is a menu associated with the current Activity

u For Android2.3.x and previous versions

Ø Pop-up through the "Menu" button of the device

Ø Appears at the bottom of the screen

u For Android3.x and later versions

Ø Appears at the right end of the ActionBar

Ø "menu" on the right end ActionBar buttons or pop-up device

Ø Menu items can appear on ActionBar

Ø

Ø

u can define the option menu in the subclass of Activity and the subclass of Fragment

ØIf both are defined, the menus of the two will be merged when displayed

Ø Display the Activity menu first when combined display

The steps to add a menu or submenu are as follows:

Ø Override the OnCreateOptionsMenu (Menu menu) method of Activiiy, call the method of the Menu object in this method to add menu items or submenus.

Ø If you want the application to respond to the click event of the menu item, override the onOptionsItemSelected (MenuItem mi) method of Activity.



public boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);this.menu = menu;return true;}

uRealize the task of adding menus through button events or other events

Ø The add method provided in the Menu class can be used to add a menu

Ø The removeItem method is provided in the Menu class to delete the menu


menu.add(groupId, itemId, order, titleRes);
Add normal menu items and bind events
public boolean onCreateOptionsMenu(Menu menu) {//Add a common menu item menu.add(0, Menu.FIRST, 1, "Add"); menu.add(0,Menu.FIRST+1,2,"Edit"); menu.add(0,Menu.FIRST+2,3,"Save"); return true; }public boolean onOptionsItemSelected(MenuItem item) {//Determine which menu item is clicked and respond switch(item.getItemId ()){ case Menu.FIRST: break; //……} return super.onOptionsItemSelected(item);}


Add submenu 


public boolean onCreateOptionsMenu(Menu menu) {SubMenu colorMenu = menu.addSubMenu("font color"); colorMenu.setIcon(R.drawable.color); // Set the icon of the menu header colorMenu.setHeaderIcon(R.drawable.color); // Set the title of the menu header colorMenu.setHeaderTitle("Select text color"); colorMenu.add(0, FONT_RED, 0, "red"); colorMenu.add(0, FONT_GREEN, 0, "green"); colorMenu. add(0, FONT_BLUE, 0, "blue"); return super.onCreateOptionsMenu(menu);}



Initialize option menu item event



u When used to select a menu item, the onOptionsItemSelected() method will execute



ØThe parameter is the object of MenuItem



Ø can be obtained by calling the menu item ID MenuItem's getItemId () method



Ø Judging and handling events based on the selected menu item ID




Create context menu






The steps to create a context menu are as follows

u rewriting Activity of onCreateContextMenu menu, View source, ContextMenu Context.MenuInfo menulnfo) method.

u Call Activity's registerForContextMenu(View view) method to register the context menu for the view component.

u If you want the application to provide a response for the menu item, you can override the OnContextItemSelected(MenuItem item) method, or bind an event listener for the specified menu item.

Add a context menu and bind events

public void onCreateContextMenu(ContextMenu menu, View source,		ContextMenu.ContextMenuInfo menuInfo)	{		menu.add(0, MENU1, 0, "红色");	}	public boolean onContextItemSelected(MenuItem mi)	{		switch (mi.getItemId())		{			……			}		return true;	}

Register the context menu for the view component

txt = (TextView) findViewById(R.id.txt);

// Register the context menu for the text box

registerForContextMenu(txt); 


XML layout menu

Layout definition of options menu

The resources of the u option menu are defined under /res/menu

uUse the <menu> tag to define the menu

uUse the <item> tag to define the menu item, the common attributes are as follows

Ø android:id defines the ID of the menu item

Ø android:icon defines the icon of the menu item

Ø android:title defines the title text of the menu item

Ø android:showAsAction defines when the menu item is displayed as an ActionItem

l取值:ifRoom | never | withText | always | collapseActionView

u

u

Example of layout definition of option menu

<menu xmlns:android="http://schemas.android.com/apk/res/android" ><itemandroid:id="@+id/action_settings"android:orderInCategory="100"android:showAsAction="never"android:icon="@drawable/home"android:title="@string/action_settings"/><itemandroid:id="@+id/action_add"android:orderInCategory="101"android:showAsAction="never"android:icon="@drawable/add"android:title="@string/action_add"/></menu>



Override onCreateOptionsMenu in Activity and load the options menu

public boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);return true;}

Control the display of menu items

uWhen the menu is laid out, you can determine whether the menu item is displayed through the attribute

ØIn many cases, we need to display menu items under special circumstances

ØInitially choose to hide the menu item

l只需要给item添加android:visible属性, l取值为false



选项菜单支持子菜单

u子菜单的定义只要在布局中的item标签内嵌入menu就可以实现

<item  android:id="@+id/action_right"android:orderInCategory="103“  android:showAsAction="never"android:icon="@drawable/right“  android:title="@string/action_right"><menu ><item android:id="@+id/action_mail” android:orderInCategory="100"android:showAsAction="never“ android:icon="@drawable/mail"android:title="@string/action_mail” />        </menu>    </item>



创建上下文菜单




<menu xmlns:android="http://schemas.android.com/apk/res/android" ><itemandroid:id="@+id/action_copy"android:orderInCategory="100"android:showAsAction="never"android:icon="@drawable/copy"android:title="@string/action_copy"/><itemandroid:id="@+id/action_past"android:orderInCategory="101"android:showAsAction="never"android:icon="@drawable/past"android:title="@string/action_past"/></menu>


单选框菜单





构建单选框菜单

u在菜单布局中有一个标签叫<group>,其常用属性如下:

Øandroid:checkableBehavior 用于定义group中菜单项的可选特征

lsingle 表示菜单项为单选

lall 表示所有菜单项都是可勾选的(复选框)

lnone 表示所有菜单项都不可勾选

u使用group标签实现单选框菜单

Ø为menu标签添加子标签group

Ø设置group的属性android:checkableBehavior=“single”

Ø在group标签中添加item标签定义单选菜单项

l在一些低的版本中,单选菜单项不能设置icon

 




构建单选框菜单

u单选框布局示例

<menu><group android:checkableBehavior="single" ><item  …… android:title="@string/action_add"/><item  …… android:title="@string/action_sub"/></group></menu>



复选框菜单


构建复选框菜单

u使用group标签实现复选框菜单

Ø为menu标签添加子标签group

Ø设置group的属性android:checkableBehavior=“all”

Ø在group标签中添加item标签定义单选菜单项

l在一些低的版本中,单选菜单项不能设置icon

u也可以直接为单个菜单项设置复选框

Ø设置item的属性android:checkable=“true”

Ø





构建复选框菜单

u复选框布局示例

<menu><group android:checkableBehavior=“all" ><item  …… android:title="@string/action_add"/><item  …… android:title="@string/action_sub"/></group></menu>


 


ActionBar概述




ActionBar诞生于Android3.0版本

u主要用于代替原来的标题栏

u可以在标题栏上展现更多的内容和功能

Ø显示选项菜单

Ø提供标签页切换方式的导航

Ø

Ø

Ø提供下拉的导航条目

Ø提供交互式活动视图代替选项条目

Ø

Ø

Ø

Ø

Ø使用程序的图标作为返回Home主屏或向上的导航操作

 




ActionBar可以带给用户明确的动作提示和全局导航

u在需要在界面上明确提示用户动作的情况下

u在需要进行全局导航的情况下

u统一显示界面重要功能

Ø比如: 搜索、新建、分享等功能出现在显著位置便于使用






在系统应用中,很多都采用了ActionBar

u比如编写新短信功能




Guess you like

Origin blog.51cto.com/2096101/2588817