android菜单_Android菜单简介

本文深入讲解Android中的三种菜单类型:弹出菜单、上下文菜单和选项菜单。详细介绍了每种菜单的用途、代码实现和XML布局配置。并探讨了如何在菜单项中添加图标,以及showAsAction属性的多种值及其含义。
摘要由CSDN通过智能技术生成

android菜单

There are three types of menus in Android: Popup, Contextual and Options.

Android中有三种类型的菜单:弹出菜单,上下文菜单和选项。

Each one has a specific use case and code that goes along with it. To learn how to use them, read on.

每个案例都有一个特定的用例以及相关的代码。 要了解如何使用它们,请继续阅读。

Each menu must have an XML file related to it which defines its layout. These are the tags associated with the menu option:

每个菜单必须具有与其相关的XML文件,该文件定义了其布局。 这些是与菜单选项关联的标签:

<menu> - This is the container element for your menu (similar to LinearLayout)

<menu> -这是<menu>的容器元素(类似于LinearLayout)

<item> - This denotes an item and is nested inside of the menu tag. Be aware that an item element can hold a <menu> element to represent a submenu

<item> -这表示一个项目,并嵌套在菜单标签内。 请注意,item元素可以包含<menu>元素来表示子菜单

<group> - This is used to signify a certain property or feature to a couple of menu items (I.E. state/visibility)

<group> -用于将某些属性或功能表示为几个菜单项(IE状态/可见性)

As shown in the code snippet above, each menu item has various attributes associated with it. I’ll detail the main ones here, but if you want to see what else you can add, go here.

如上面的代码片段所示,每个菜单项都具有与之关联的各种属性。 我将在此处详细说明主要的内容,但是如果您想查看其他可以添加的内容,请转到此处

  • id - This is a unique identifier for the item in the menu. You can use this to see exactly which item the user clicked

    id-这是菜单中项目的唯一标识符。 您可以使用它来准确查看用户单击了哪个项目

  • icon - If you want to show an icon associated with that menu item

    图标 -如果要显示与该菜单项关联的图标

  • title - Text that will be shown in the menu for that item

    标题 -文本将在菜单中显示该项目被

  • showAsAction - This attribute should only be used when using a menu in an activity that uses an application bar(or as it is also referred to, the action bar). It controls when and how this item should appear as an action in the application bar. There are five values: always, never, ifRoom, withText, and collapseActionView

    showAsAction-仅当在使用应用程序栏 (或也称为操作栏)的活动中使用菜单时,才应使用此属性。 它控制该项目何时以及如何在应用程序栏中以动作形式显示。 有五个值:always,never,ifRoom,withText和collapseActionView

android:showAsAction="always|never|ifRoom|withText|collapseActionView"

I’ll elaborate on the meaning of each of these values in the next section.

在下一节中,我将详细说明每个值的含义。

In addition, you need to add the relevant onCreate menu method to your activity.

另外,您需要向活动中添加相关的onCreate菜单方法。

选项菜单 (Options Menu)

This menu is usually found at the top of your application and in it, you should place actions that affect the application as a whole. These could be the application’s settings or a search box.

此菜单通常位于应用程序的顶部,您应该在其中放置影响整个应用程序的操作。 这些可以是应用程序的设置或搜索框。

Using the menu layout from above, we get the following options menu:

使用上面的菜单布局,我们获得以下选项菜单:

As promised, let’s go over the values that can be given for the showAsAction attribute:

如所承诺的,让我们看一下可以为showAsAction属性指定的值:

  • always - will always show in the action bar

    始终-始终显示在操作栏中
  • never - will never show, and therefore will be available through the overflow menu

    从不-从不显示,因此可通过溢出菜单使用

  • ifRoom - only if there is sufficient space in the action bar, then it would be shown. Keep in mind that per the documentation, there is a limit to how many icons you can have on the action bar.

    ifRoom-仅在操作栏中有足够的空间时,才会显示它。 请记住,根据文档,操作栏上可以有多少个图标是有限制的。
  • withText-will include the item’s title in the action bar

    withText-将在操作栏中包含该项目的标题
  • collapseActionView - if this item has an action view associated with it, it will become collapsible(from API 14 and above)

    crashActionView-如果此项目具有与之关联的动作视图,它将变为可折叠的(从API 14及更高版本开始)

If we go ahead and change the last item in our menu to showAsAction=”never”, we get the following:

如果继续进行,并将菜单中的最后一项更改为showAsAction =“ never”则会得到以下信息:

上下文菜单 (Contextual Menu)

This menu appears when a user performs a long click on one of your UI elements. The options found in this menu affect what UI element the user made the click on. It is common to use this type of menu in list or grid views, where the user’s interaction with each item can lead to a specific action.

当用户在您的UI元素之一上长按时,将显示此菜单。 此菜单中的选项会影响用户单击的UI元素。 通常在列表视图或网格视图中使用这种类型的菜单,其中用户与每个项目的交互都可以导致特定的操作。

Imagine a scenario where you have an application with an image, and you want to present to the user several choices when they click on the image.

想象一下一个场景,其中您有一个带有图像的应用程序,并且您想在用户单击图像时向用户显示多个选择。

A context menu can appear in two ways :

上下文菜单可以通过两种方式出现:

  1. A floating menu

    浮动菜单
  2. An action bar at the top of your application

    应用程序顶部的操作栏

We will only demonstrate how to use the first option, but you can read more about the second option here.

我们将仅演示如何使用第一个选项,但是您可以在此处阅读有关第二个选项的更多信息。

Using the following XML:

使用以下XML:

And adding the following code to our main activity:

并将以下代码添加到我们的主要活动中:

We will get the following:

我们将获得以下内容:

A popup menu is a type of menu that displays items in a vertical list. This list is attached to the view the user has clicked on to invoke this menu. It is important to keep in mind, that when choosing a popup menu, you do not want the user’s choice to affect the previous content the user pressed.

弹出菜单是一种菜单,可以在垂直列表中显示项目。 此列表附加到用户单击以调用此菜单的视图。 重要的是要记住,选择弹出菜单时,您不希望用户的选择影响用户按下的先前内容。

We will use the same menu XML layout as before, but we will need to add the following code to our activity:

我们将使用与以前相同的菜单XML布局,但是我们需要在活动中添加以下代码:

We will get the same result as the previous screenshot, but without the need for the user to perform a long click.

我们将获得与之前的屏幕截图相同的结果,但无需用户长按。

弹出菜单中的图标 (Icons In Popup Menus)

Now I know what you are probably here for: you want to know how you can add icons to the menus.

现在,我知道您可能在这里做什么: 您想知道如何在菜单中添加图标

While I will show an example of how to do this, it is wise to understand that this is a feature that is not enabled for popup menus and may cause unexpected behavior. You can achieve this by using reflection to turn on a flag called setForceShowIcon.

虽然我将显示一个有关如何执行此操作的示例,但明智的理解是此功能尚未启用弹出菜单,并且可能导致意外行为。 您可以通过使用反射打开一个名为setForceShowIcon的标志来实现

//popup is an instance of PopupMenu

try {
      Field[] fields = popup.getClass().getDeclaredFields();
      for (Field field : fields) {
          if ("mPopup".equals(field.getName())) {
              field.setAccessible(true);
              Object menuPopupHelper = field.get(popup);
              Class<?> classPopupHelper = Class.forName(menuPopupHelper
                      .getClass().getName());
              Method setForceIcons = classPopupHelper.getMethod(
                      "setForceShowIcon", boolean.class);
              setForceIcons.invoke(menuPopupHelper, true);
              break;
          }
      }
  } catch (Throwable e) {
      e.printStackTrace();
  }

I’ve just scratched the surface with Android menus, but hopefully, it is enough to inspire you to dig deeper.

我只是从Android菜单开始,但希望它足以激发您进行更深入的研究。

翻译自: https://www.freecodecamp.org/news/android-menus-introduction/

android菜单

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值