Setting a custom share icon on Actionbar ShareActionProvider

有些话、适合烂在心里 提交于 2019-11-30 11:39:59

When I am looking to customize the action bar, usually the first place I look is the ActionBarSherlock themes and styles.

I found that the Sherlock theme uses the "actionModeShareDrawable" as found in theme.xml file.

Try changing your theme to include the "actionModeShareDrawable" item directly.

<style name="Theme.MyApp" parent="Theme.Sherlock.Light">
    <item name="actionModeShareDrawable">@drawable/ic_action_share</item>
</style>

For AppCompat the changes to 2 styles are needed:

1)

<style name="MyAppTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="actionBarWidgetTheme">@style/Theme.AppCompat.CustomShareIcon</item>
    <item name="android:actionBarWidgetTheme">@style/Theme.AppCompat.CustomShareIcon</item>
</style>

In other words, specifiing attr:actionModeShareDrawable in your MyAppTheme is not what you really need, but it should be mentioned in your attr:actionBarWidgetTheme as following:

2)

<style name="Theme.AppCompat.CustomShareIcon" parent="@style/Theme.AppCompat">
    <item name="actionModeShareDrawable">@drawable/abc_ic_menu_share_holo_dark</item> <!-- your icon goes here -->
</style>

UPDATE: Forget about the TRICKY way below, just sub-class ShareActionProvider, return null in onCreateActionView Method, and provide your own icon in menu.xml file, everything will be fine

===========================

I found a very tricky but none-ActionBarSherlock-specific way:

  1. Sub-class ShareActionProvider, with just a little tweak:

    @Override
    public View onCreateActionView() {
        View target = super.onCreateActionView();
        ViewGroup viewGroup = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.actionbutton_share, null);
        ImageView overlay = (ImageView) viewGroup.findViewById(R.id.overlay);
        ViewGroup container = (ViewGroup) viewGroup.findViewById(R.id.container);
        container.addView(target);
        container.getLayoutParams().width = overlay.getLayoutParams().width;
        container.getLayoutParams().height = overlay.getLayoutParams().height;
        return viewGroup;
    }
    
  2. Create a layout file (R.layout.actionbutton_share), which place the original view at top but transparent to user (Note the "android:alpha=0" part) :

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <FrameLayout
            android:id="@+id/container"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:alpha="0" />
    
        <ImageView
            android:id="@+id/overlay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="7dp"
            android:layout_marginRight="7dp"
            android:src="@drawable/yours_whatever_share_icon"
            tools:ignore="ContentDescription" />
    
    </RelativeLayout>
    
  3. Use the hacked ShareActionProvider in your menu.xml file

vivimice's solution is good, but the icon of last used app for sharing becomes hidden too.

I did custom share icon as a common menu item without any ShareActionProvider, just in onOptionsItemSelected() method used this code:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));

It creates not a popup menu, but a chooser dialog.

You can override the non-public attribute like this.

<style name="Theme.MyApp" parent="android:Theme.Holo">
        <item name="*android:actionModeShareDrawalbe">@drawable/icon</item>
</style>

(*) means reference to the non-public resources in Android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!