问题
My app's primary color is some kind of yellow. The ShareActionProvider in the action bar has a kind of spinner that when pressed shows the default holo blue color.
To make the action bar style coherent, I need to have the share button background go yellow when pressed. How to achieve that?
I am using the standard action bar (no compatibility or sherlock)
That one does not work:
shareMenuItem.getActionView().setBackgroundResource(R.drawable.spinner_background_ab_webcamhd_custom_ab);
And none of the questions on SO about styling the ShareActionProvider got any answer :(
回答1:
I wanted white share button with a white background to popup. So first I changed the textColorSecondary in my main theme to white.
<style name="MainTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/appprimary</item>
<item name="colorPrimaryDark">@color/appprimaryDark</item>
<item name="colorAccent">@color/appaccent</item>
<item name="android:textColorPrimary">#ffffff</item>
<item name="android:textColorSecondary">#ffffff</item>
<item name="android:textColor">#212121</item>
<item name="listPopupWindowStyle">@style/PopupListStyle</item>
</style>
Then I used a custom style for the popup as given by intips' answer:
<style name="PopupListStyle" parent="@style/Widget.AppCompat.Light.ListPopupWindow">
<item name="android:popupBackground">#ffffff</item>
</style>
And then in my toolbar I set this theme:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:minHeight="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:theme="@style/MainTheme">
回答2:
I added this to my theme and it worked:
<item name="listPopupWindowStyle">@style/Widget.AppCompat.Light.ListPopupWindow</item>
So if you want to customize the colors you need to create your own style
回答3:
Doesn't the ShareActionProvider use the standard Action Bar background? If so, you can control this by overriding the resource used in your theme. E.g.
<style name="AppTheme" parent="android:Theme.Holo.Light">
<item name="android:actionBarItemBackground">@drawable/your_selector</item>
</style>
For an example on how to build the XML Selector Drawable, check out the one used by the system , like this.
Update
How to find this yourself.
- Look up source of
SharedActionProviderand examineonCreateActionView(). - Follow to
ActivityChooserViewwhich is theViewcreated inonCreateActionView(). - Observe the layout inflated in the constructor of
ActivityChooserView, in this caseR.layout.activity_chooser_view. - Look up source of
R.layout.activity_chooser_viewand find the layout used for the button, in this case@+id/expand_activities_button. - Observe the resource used for the background, in this case,
android:background="?android:attr/actionBarItemBackground"
回答4:
For anyone who has the same problem this is what worked for me:
-in AppTheme add:
<item name="listPopupWindowStyle">@style/PopupListStyle</item>
-create new style for listpopup:
<style name="PopupListStyle" parent="@style/Widget.AppCompat.Light.ListPopupWindow">
<item name="android:popupBackground">@color/list_popup_bg</item>
</style>
来源:https://stackoverflow.com/questions/19134083/change-android-shareactionprovider-background-color-when-pressed