I'm trying to set the icon ShareActionProvider, need a solid white one instead of the semi transparent white one.
However setting in share_menu.xml and code doesn't work. Just wondering whether anyone has got workaround for this before extending the ShareActionProvider. (I'm using actionbarsherlock)
@Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
inflater.inflate(R.menu.share_menu, menu);
MenuItem actionItem = menu.findItem(R.id.share_action_provider);
ShareActionProvider actionProvider = (ShareActionProvider) actionItem.getActionProvider();
actionProvider.setShareIntent(mShareIntent);
// re-setting the icon as it's not being picked up from the menu xml file
actionItem.setIcon(R.drawable.ic_action_share);
super.onCreateOptionsMenu(menu, inflater);
}
share_menu.xml
<item android:id="@+id/share_action_provider"
android:showAsAction="ifRoom"
android:title="@string/share_with"
android:icon="@drawable/ic_action_share"
android:actionProviderClass="com.actionbarsherlock.widget.ShareActionProvider" />
Update: Also tried setting it in the style, but no joy
<style name="Theme.MyApp" parent="Theme.Sherlock.Light">
<item name="actionModeStyle">@style/ActionModeStyle</item>
<item name="android:actionModeStyle">@style/ActionModeStyle</item>
</style>
<style name="ActionModeStyle" parent="Widget.Sherlock.ActionMode">
<item name="actionModeShareDrawable">@drawable/ic_action_share</item>
</style>
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:
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; }
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>
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
来源:https://stackoverflow.com/questions/10832108/setting-a-custom-share-icon-on-actionbar-shareactionprovider