Android官方文档翻译 九 2.2Adding Action Buttons

Adding Action Buttons

增加动作按钮

This lesson teaches you to

这节课教给你

  1. Specify the Actions in XML

    在XML中指定动作

  2. Add the Actions to the Action Bar

    把动作添加到状态栏

  3. Respond to Action Buttons

    让动作按钮有响应

  4. Add Up Button for Low-level Activities

    对低版本的activities增加顶部按钮

You should also read

你还应该读


  • Providing Up Navigation

提供顶部导航


The action bar allows you to add buttons for the most important action items relating to the app’s current context. Those that appear directly in the action bar with an icon and/or text are known as action buttons. Actions that can’t fit in the action bar or aren’t important enough are hidden in the action overflow.

状态栏允许你把大部分重要的动作项目的按钮添加在其上以和应用程序的当前上下文环境相关联。这些可以直接在状态栏上显示一个用来辨别动作按钮的图标或者文本。在状态栏上装不下的动作按钮或者不是足够重要的动作按钮可以隐藏在动作溢出夹(更多操作)中。

Figure 1. An action bar with an action button for Search and the action overflow, which reveals additional actions.

图1 拥有一个查找动作按钮和一个动作溢出夹(更多操作)的一个状态栏,它提供了额外的动作。

Specify the Actions in XML

在XML中指定动作

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.

所有的动作按钮和其它一些放在动作溢出夹(更多操作)中的可用项目都需要在一个XML菜单资源中定义。为了在状态栏上添加动作,在你的项目的 res/menu/目录下创建一个新的XML文件。

Add an element for each item you want to include in the action bar. For example:

为你想要在状态栏中添加的每个项目增加一个元素。例如:

res/menu/main_activity_actions.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- Search, should appear as action button -->
    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          android:showAsAction="ifRoom" />
    <!-- Settings, should always be in the overflow -->
    <item android:id="@+id/action_settings"
          android:title="@string/action_settings"
          android:showAsAction="never" />
</menu>

Download action bar icons

下载状态栏图标

To best match the Android iconography guidelines, you should use icons provided in the Action Bar Icon Pack.

为了最好的适配Android图解向导,你应该使用状态栏图标包里提供的图标。(官方网站可以下载)

This declares that the Search action should appear as an action button when room is available in the action bar, but the Settings action should always appear in the overflow. (By default, all actions appear in the overflow, but it’s good practice to explicitly declare your design intentions for each action.)

这里声明:当状态栏有空间可用时显示搜索按钮,但是设置按钮应该总是在隐藏夹中出现。(默认情况下,所有的动作都在隐藏夹中出现,但是这对于你显式的声明你对每个动作的设计意图是很好的一个练习。)

The icon attribute requires a resource ID for an image. The name that follows @drawable/ must be the name of a bitmap image you’ve saved in your project’s res/drawable/ directory. For example, “@drawable/ic_action_search” refers to ic_action_search.png. Likewise, the title attribute uses a string resource that’s defined by an XML file in your project’s res/values/ directory, as discussed in Building a Simple User Interface.

icon 属性需要一个图片资源ID。它的名字跟在 @drawable/后面,这个名字必须是你已经在你的项目的res/drawable/ 目录下保存的一个位图图像的名字。例如,“@drawable/ic_action_search”引用的是ic_action_search.png这张图片。同样地,title属性使用一个string资源,这个资源也是你在你的项目的res/values/目录下的一个XML文件中定义好的,就如我们之前在Building a Simple User Interface中讨论的那样。

Note: When creating icons and other bitmap images for your app, it’s important that you provide multiple versions that are each optimized for a different screen density. This is discussed more in the lesson about Supporting Different Screens.

注意:当你在你的应用程序中创建图标和其他的位图图像时,对于不同的屏幕密度的设备提供多种版本用来优化是很重要的。这在课程Supporting Different Screens课程中将会学到更多。

If your app is using the Support Library for compatibility on versions as low as Android 2.1, the showAsAction attribute is not available from the android: namespace. Instead this attribute is provided by the Support Library and you must define your own XML namespace and use that namespace as the attribute prefix. (A custom XML namespace should be based on your app name, but it can be any name you want and is only accessible within the scope of the file in which you declare it.) For example:

如果你的应用程序为了兼容Android 2.1以下版本而使用了Support Library,在android:中的命名空间中showAsAction属性是不可用的。Support Library提供了这个属性用来代替,你必须定义你自己的XML命名空间,然后使用这个命名空间作为这个属性的前缀。(一个自定义的XML命名空间应该以你的应用程序名字为基础,但是它可以使用任何你想用的名字来定义,并且它仅仅在你生命的这个文件范围内才能访问。)例如:

res/menu/main_activity_actions.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
    <!-- Search, should appear as action button -->
    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          yourapp:showAsAction="ifRoom"  />
    ...
</menu>

Add the Actions to the Action Bar

给状态栏添加动作

To place the menu items into the action bar, implement the onCreateOptionsMenu() callback method in your activity to inflate the menu resource into the given Menu object. For example:

为了把菜单项目放置到状态栏上,需要在你的activity中实现onCreateOptionsMenu()这个回调方法,以把菜单资源加载到给定的菜单对象上。例如:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu items for use in the action bar
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_activity_actions, menu);
    return super.onCreateOptionsMenu(menu);
}

Respond to Action Buttons

对动作按钮做出响应

When the user presses one of the action buttons or another item in the action overflow, the system calls your activity’s onOptionsItemSelected() callback method. In your implementation of this method, call getItemId() on the given MenuItem to determine which item was pressed—the returned ID matches the value you declared in the corresponding element’s android:id attribute.

当用户点击其中一个动作按钮或者动作溢出栏(更多操作)中的一个项目时,系统会调用你的activity中的onCreateItemSelected()回调方法。在这个方法中你要实现的是,在给定的菜单条目中调用getItemId()来确定哪个条目被点击了—-返回的ID和你在相应的元素中的android:id 属性定义的值相匹配。

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.action_search:
            openSearch();
            return true;
        case R.id.action_settings:
            openSettings();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

Add Up Button for Low-level Activities

对低版本的activities增加顶部按钮

Figure 4. The Up button in Gmail.

图4 Gmail中的顶部按钮

All screens in your app that are not the main entrance to your app (activities that are not the “home” screen) should offer the user a way to navigate to the logical parent screen in the app’s hierarchy by pressing the Up button in the action bar.

在你的应用程序中,对于所有不是主入口的屏幕(activities不是“home”屏幕)都应该提供给用户一个方式导航跳转到它的逻辑父屏幕,这些要通过在工具栏上点击按钮来实现应用程序中的这种层次结构。

When running on Android 4.1 (API level 16) or higher, or when using ActionBarActivity from the Support Library, performing Up navigation simply requires that you declare the parent activity in the manifest file and enable the Up button for the action bar.

当在Android 4.1(API 16)及其以上运行,或者当使用Support Library提供的ActionBarActivity时,执行顶部的导航菜单仅仅需要你在manifest文件中定义它的父activity,并且让其成为工具栏的顶部按钮。

For example, here’s how you can declare an activity’s parent in the manifest:

例如,以下是你如何在manifest中定义一个activity的父亲:

<application ... >
    ...
    <!-- The main/home activity (it has no parent activity) -->
    <activity
        android:name="com.example.myfirstapp.MainActivity" ...>
        ...
    </activity>
    <!-- A child of the main activity -->
    <activity
        android:name="com.example.myfirstapp.DisplayMessageActivity"
        android:label="@string/title_activity_display_message"
        android:parentActivityName="com.example.myfirstapp.MainActivity" >
        <!-- Parent activity meta-data to support 4.0 and lower -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.myfirstapp.MainActivity" />
    </activity>
</application>

Then enable the app icon as the Up button by calling setDisplayHomeAsUpEnabled():

然后通过调用setDisplayHomeAsUpEnabled()让其可用app的图标作为顶部按钮:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_displaymessage);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    // If your minSdkVersion is 11 or higher, instead use:
    // getActionBar().setDisplayHomeAsUpEnabled(true);
}

Because the system now knows MainActivity is the parent activity for DisplayMessageActivity, when the user presses the Up button, the system navigates to the parent activity as appropriate—you do not need to handle the Up button’s event.

因为现在系统知道了MainActivity是DisplayMessageActivity的父activity,所以当用户点击顶部按钮时,系统会视情况导航跳转到它的父activity—-你根本不需要处理顶部按钮的事件。

For more information about up navigation, see Providing Up Navigation.

想知道关于顶部导航的更多信息,请看Providing Up Navigation。

NEXT: STYLING THE ACTION BAR

下一节:给菜单栏设计样式

这些是我自己翻译的,如果您发现其中有重要错误,敬请指出,万分感谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值