ActionBar(官方文档学习续(二)。。)Adding and Handling Actions

Adding and Handling Actions

添加和处理操作

The app bar allows you to add buttons for user actions. This feature lets you put the most important actions for the current context right at the top of the app. For example, a photo browsing app might show share and create album buttons at the top when the user is looking at their photo roll; when the user looks at an individual photo, the app might show crop and filterbuttons.

应用程序栏允许你为用户添加一个按钮,这个特性 让你把最重要的actions设置在当前应用程序上下文的右边。例如,当用户在看他们的照片时,一个照片浏览应用程序可能会显示债片的分享和创建相册的按钮放在顶部;当用户在看个人的照片时,应用程序可能显示复制和过滤按钮。

Space in the app bar is limited. If an app declares more actions than can fit in the app bar, the app bar send the excess actions to an overflow menu. The app can also specify that an action should always be shown in the overflow menu, instead of being displayed on the app bar.

应用程序栏的控件是有限的。如果一个应用程序描述actions超过了应用程序栏可以容纳的空间。这个应用程序栏发送者多出来的actions到一个菜单。这应用程序也可以指定一个action总是在菜单显示。而不是显示在应用程序栏。

 

Figure 1. An app bar with a single action button and an overflow menu.

 

Add Action Buttons

All action buttons and other items available in the action overflow are defined in an XML menu resource. To add actions to the action bar, create a new XML file in your project's res/menu/ directory.

在action中所有的action按钮和其他的可用的项都被定义在一个xml menu源文件中。在res/menu/目录下创建一个新的xml文件去添加一个操作到操作栏中。

Add an <item> element for each item you want to include in the action bar, as shown in this code example of a menu XML file:

为每个你想添加到操作栏中的项添加一个<item>元素

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- "Mark Favorite", should appear as action button if possible -->
    <item
        android:id="@+id/action_favorite"
        android:icon="@drawable/ic_favorite_black_48dp"
        android:title="@string/action_favorite"
        app:showAsAction="ifRoom"/>

    <!-- Settings, should always be in the overflow -->
    <item android:id="@+id/action_settings"
          android:title="@string/action_settings"
          app:showAsAction="never"/>

</menu>

The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar. If you set app:showAsAction="ifRoom" (as in the example code's favorite action), the action is displayed as a button if there is room in the app bar for it; if there is not enough room, excess actions are sent to the overflow menu. If you set app:showAsAction="never" (as in the example code's settings action), the action is always listed in the overflow menu, not displayed in the app bar.

app:showAsAction 属性指定操作是否应该作为按钮显示在应用程序栏上。如果你设置app:showAsAction =“ifRoom”,如果应用程序控制栏给予该操作控件,这个操作显示一个按钮。如果控件不足。多出的操作会显示在菜单按钮中。如果你设置app:showAsAction="never",这个操作就会一直显示在菜单中,不是显示在应用程序操作栏上

The system uses the action's icon as the action button if the action is displayed in the app bar. You can find many useful icons on the Material Icons page.

如果操作在应用程序中显示,系统使用操作的图标作为这个操作按钮。你可以在Materal Icons 页面找到许多有用的图标。

Respond to Actions

响应操作

When the user selects one of the app bar items, the system calls your activity's onOptionsItemSelected()callback method, and passes a MenuItem object to indicate which item was clicked. In your implementation ofonOptionsItemSelected(), call the MenuItem.getItemId() method to determine which item was pressed. The ID returned matches the value you declared in the corresponding <item> element's android:id attribute.

当用户选择一个应用程序栏的项时,系统调用你的activity 中的onOptionsItemSelected()方法。通过一个MenuItemd对象去表明item被点击了,在你实现onOptionsItemSelected()方法中,调用MenuItem.getItemId() 方法去确定那个项被按下,返回的ID将匹配你在<item>元素中声明的android:id的值。

For example, the following code checks to see which action the user selected. If the method does not recognize the user's action, it invokes the superclass method:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_settings:
            // User chose the "Settings" item, show the app settings UI...
            return true;

        case R.id.action_favorite:
            // User chose the "Favorite" action, mark the current item
            // as a favorite...
            return true;

        default:
            // If we got here, the user's action was not recognized.
            // Invoke the superclass to handle it.
            return super.onOptionsItemSelected(item);

    }
}

 

你可能感兴趣的:(ActionBar(官方文档学习续(二)。。)Adding and Handling Actions)