简体   繁体   中英

How to fix the share icon in the support actionbar

I'm trying to implement the share functionality within my app. So far it works fine and I can share text to all other apps. The problem is the way it's shown.

I want something like just the share icon visible and then when user taps on it, it opens the OS dialog and lets user choose the app they want to share content to.

    var share_article = menu.FindItem (Resource.Id.action_share);
    var share_article_provider = (Android.Support.V7.Widget.ShareActionProvider) Android.Support.V4.View.MenuItemCompat.GetActionProvider (share_article);
    share_article_provider.SetShareIntent (CreateIntent ());

and the xml:

<item 
    android:id="@+id/action_share"
    myapp:showAsAction="ifRoom"
    android:title="share"
    myapp:actionProviderClass="android.support.v7.widget.ShareActionProvider" />

My app currently looks like this:

在此处输入图片说明

There's also a white border around it that I don't like. Is there any way to change the icon??

How do I fix it??

You just want to turn off your share history.There is no official API to do this, but you can make your own ShareActionProvider . Actually there are two similar question on SO:

  1. How do you turn off share history when using ShareActionProvider?
  2. How to hide the share action (which use most) icon near the share action provider?

Wish these could help you.

As mentioned here when using support library this can be fixed really easily. This method won't turn off the share history but will hide the icons from actionbar. I just needed to subclass the Android.Support.V7.Widget.ShareActionProvider like the following: (C# using Xamarin)

public class MyShareActionProvider : Android.Support.V7.Widget.ShareActionProvider
{
    public SingleArticleShareActionProvider (Context context) : base (context)
    {}

    public override View OnCreateActionView ()
    {
        return null;
    }
}

and then inside OnCreateOptionsMenu use the MyShareActionProvider like:

var share_article = menu.FindItem (Resource.Id.action_share);
var share = new SingleArticleShareActionProvider (globalContext);

Android.Support.V4.View.MenuItemCompat.SetActionProvider (share_article, share);
share_article.SetIcon (Resource.Drawable.abc_ic_menu_share_mtrl_alpha);
share.SetShareIntent (CreateIntent ());

You can use any icon you like with the method SetIcon .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM