问题
I have the same problem as was described here - Setting a custom share icon on Actionbar ShareActionProvider
But I'am not using ActionBarSherlock
I found that the Sherlock theme uses the "actionModeShareDrawable" and I can also use it like this, if I don't use ActionBarSherlock
<style name="Theme.MyApp" parent="android:Theme.Holo">
<item name="*android:actionModeShareDrawable">@drawable/icon</item>
</style>
This works fine on my nexus 5, but failed on many other devices
So my question is, how to change that icon without using ActionBarSherlock
回答1:
You can subclass ShareActionProvider, overriding only the constructor and createActionView().
From here, you can get the View from super, casting it to ActivityChooserView so you can call setExpandActivityOverflowButtonDrawable(Drawable) to change the icon.
package com.yourpackagename.whatever;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.internal.widget.ActivityChooserView;
import android.support.v7.widget.ShareActionProvider;
import android.view.View;
import com.yourpackagename.R;
public class CustomShareActionProvider extends ShareActionProvider {
private final Context mContext;
public CustomShareActionProvider(Context context) {
super(context);
mContext = context;
}
@Override
public View onCreateActionView() {
ActivityChooserView chooserView =
(ActivityChooserView) super.onCreateActionView();
// Set your drawable here
Drawable icon =
mContext.getResources().getDrawable(R.drawable.ic_action_share);
chooserView.setExpandActivityOverflowButtonDrawable(icon);
return chooserView;
}
}
回答2:
I like PrplRugby's answer, but you have to include the ActivityChooserView in your app. I refactored to use reflection which you can find here: https://gist.github.com/briangriffey/11185716
回答3:
You can use in the menu like this
menu.xml
<item android:id="@+id/menu_share"
android:title="@string/share"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
app:showAsAction="always"
android:orderInCategory="100"/>
And into your set icon if api>21 android:actionModeShareDrawable otherwise actionModeShareDrawable
Style.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
....
<item name="actionModeShareDrawable">@drawable/share</item>
</style>
In the last apply this theme
manifest.xml
<application
...
android:theme="@style/AppTheme" >
回答4:
I know this is not in a theme but I set my share icon in menu.xml. This may help if you are just trying to have a custom icon.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/share" android:showAsAction="always" android:title="@string/share" android:icon="@drawable/ic_menu_share"/>
</menu>
I am surprised it works at all. Try changing actionModeShareDrawalbe to actionModeShareDrawable.
<style name="Theme.MyApp" parent="android:Theme.Holo">
<item name="android:actionModeShareDrawable">@drawable/icon</item>
</style>
来源:https://stackoverflow.com/questions/21377816/setting-a-custom-share-icon-on-actionbar-shareactionprovider-without-actionbarsh