Google Android开发者文档系列-创建有内容分享特性的应用之添加一个简单的共享action

来源:互联网 发布:域名哪里买 编辑:程序博客网 时间:2024/05/17 08:26

Adding an Easy Share Action(添加一个简单的共享action)

该系列文章是我在学习Google开发者文档时结合谷歌翻译和自身理解编写的,希望对学习Android开发的朋友带来些便利,由于个人翻译水平有限,所以内容包含原文和译文,希望浏览者结合理解,以免步入我可能错译的误区。在此感谢http://android.xsoftlab.net/提供的镜像,希望转载者注明出处http://blog.csdn.net/u014031072/article/details/51594602方便查看最新博客

Implementing an effective and user friendly share action in your ActionBar is made even easier with the introduction of ActionProvider in Android 4.0 (API Level 14). An ActionProvider, once attached to a menu item in the action bar, handles both the appearance and behavior of that item. In the case of ShareActionProvider, you provide a share intent and it does the rest.
通过在Android4.0(API Level 14)中对ActionBar的介绍。在ActionBar中实现一个有效并用户友好的分享action变得更加简单。一个ActionProvider,一旦附着到action bar的菜单项中,将会同时处理该项目的外观和行为。在ShareActionProvider的情况下,你提供了一个分享的intent,它来完成剩余的操作。

Note: ShareActionProvider is available starting with API Level 14 and higher.
注:ShareActionProvider 从API Level 14或者更高版本可用。

Update Menu Declarations(更新菜单定义)

To get started with ShareActionProviders, define the android:actionProviderClass attribute for the corresponding in your menu resource file:
开始使用ShareActionProvider,先在菜单资源文件中对应的<?(去掉问号)item>要素下定义android:actionProviderClass属性:

<menu xmlns:android="http://schemas.android.com/apk/res/android">    <item            android:id="@+id/menu_item_share"            android:showAsAction="ifRoom"            android:title="Share"            android:actionProviderClass=                "android.widget.ShareActionProvider" />    ...</menu>

This delegates responsibility for the item’s appearance and function to ShareActionProvider. However, you will need to tell the provider what you would like to share.
这代表ShareActionProvider负责处理该项的外观和功能。但是,你需要告诉provider 你想分享的内容。

Set the Share Intent(设置分享intent)

In order for ShareActionProvider to function, you must provide it a share intent. This share intent should be the same as described in the Sending Simple Data to Other Apps lesson, with action ACTION_SEND and additional data set via extras like EXTRA_TEXT and EXTRA_STREAM. To assign a share intent, first find the corresponding MenuItem while inflating your menu resource in your Activity or Fragment. Next, call MenuItem.getActionProvider() to retrieve an instance of ShareActionProvider. Use setShareIntent() to update the share intent associated with that action item. Here’s an example:
为了使ShareActionProvider 正常工作,你必须给它提供一个分享intent。这个分享intent应该跟Sending Simple Data to Other Apps课程中描述的一样,包括action ACTION_SEND 和通过像设置EXTRA_TEXT 和EXTRA_STREAM方式附加的数据。要指定一个分享intent,首先在你的activity或者fragment填充菜单资源时获取到对应的菜单项,然后调用MenuItem.getActionProvider()方法来获取一个ShareActionProvider实例。使用setShareIntent()方法来更新对应动作项的分享intent。下面是一个例子:

private ShareActionProvider mShareActionProvider;...@Overridepublic boolean onCreateOptionsMenu(Menu menu) {    // Inflate menu resource file.    //填充菜单资源文件    getMenuInflater().inflate(R.menu.share_menu, menu);    // Locate MenuItem with ShareActionProvider    //找到ShareActionProvider对应的菜单项    MenuItem item = menu.findItem(R.id.menu_item_share);    // Fetch and store ShareActionProvider    //获取并保存ShareActionProvider    mShareActionProvider = (ShareActionProvider) item.getActionProvider();    // Return true to display menu    //返回true来显示菜单    return true;}// Call to update the share intent//用来更新分享intent的方法private void setShareIntent(Intent shareIntent) {    if (mShareActionProvider != null) {        mShareActionProvider.setShareIntent(shareIntent);    }}

You may only need to set the share intent once during the creation of your menus, or you may want to set it and then update it as the UI changes. For example, when you view photos full screen in the Gallery app, the sharing intent changes as you flip between photos.
在创建你的菜单时你可能只需要设置一次分享intent,或者你可能想在UI改变时来动态更新它。比如,当你在图库应用里全屏查看照片时,但你在不同照片间滑动时分享intent会动态改变。

For further discussion about the ShareActionProvider object, see the Action Bar guide.
了解更多关于ShareActionProvider的内容,请查看Action Bar 引导。

0 0
原创粉丝点击