如何在android studion中添加两个onCreateOptionsMenu(菜单菜单)

时间:2016-06-28 12:41:54

标签: android share oncreateoptionsmenu

我正在创建一个Android应用程序,其中我已准备好添加共享选项以共享应用程序的内容,但我想添加另一个共享选项,它将能够共享应用程序下载链接(分享此应用程序),两者选项用于创建选项菜单,有人可以告诉我是否可以在创建选项上添加两个或是否有其他方式添加第二个共享操作。 以下是我用于“分享此应用”操作的代码。

 private ShareActionProvider mShareActionProvider;
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    /** Inflating the current activity's menu with res/menu/items.xml */
    getMenuInflater().inflate(R.menu.menu_main, menu);

    /** Getting the actionprovider associated with the menu item whose id is share */
    mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share).getActionProvider();

    /** Setting a share intent */
    mShareActionProvider.setShareIntent(getDefaultShareIntent());

    return super.onCreateOptionsMenu(menu);

}

/** Returns a share intent */
private Intent getDefaultShareIntent(){
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_SUBJECT, "download the app");
    intent.putExtra(Intent.EXTRA_TEXT," play.google.com ");
    return intent;
}

menu_main

<item
    android:title="Share"
    android:id="@+id/menu_item_share"
    android:showAsAction="ifRoom"
    android:icon="@drawable/share"
    />

<item
    android:id="@+id/share_this_app"
    android:title="share this app"
    android:showAsAction="never"
    android:actionProviderClass="android.widget.ShareActionProvider"/>

1 个答案:

答案 0 :(得分:0)

你尝试过这样的事吗?如果有效,请告诉我。

private ShareActionProvider mShareActionProvider;
private ShareActionProvider mShareActionProvider2;

@Override
public boolean onCreateOptionsMenu(Menu menu) {    
    /** Inflating the current activity's menu with res/menu/items.xml */
    getMenuInflater().inflate(R.menu.menu_main, menu);

    /** Getting the actionprovider associated with the menu item whose id is share */
    mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.menu_item_share).getActionProvider();
    mShareActionProvider2 = (ShareActionProvider) menu.findItem(R.id.share_this_app).getActionProvider();

    /** Setting a share intent */
    mShareActionProvider.setShareIntent(getDefaultShareIntent());
    mShareActionProvider2.setShareIntent(getDefaultShareIntent2());

    return super.onCreateOptionsMenu(menu);

}

/** Returns a share intent */
private Intent getDefaultShareIntent(){
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_SUBJECT, "download the app");
    intent.putExtra(Intent.EXTRA_TEXT," play.google.com ");
    return intent;
}

private Intent getDefaultShareIntent2(){
    /*Your coude here*/
}