Change Android ShareActionProvider background colo

2019-05-11 20:02发布

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 :(

4条回答
Luminary・发光体
2楼-- · 2019-05-11 20:50

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楼-- · 2019-05-11 20:51

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">

enter image description here

查看更多
叼着烟拽天下
4楼-- · 2019-05-11 20:51

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.

  1. Look up source of SharedActionProvider and examine onCreateActionView().
  2. Follow to ActivityChooserView which is the View created in onCreateActionView().
  3. Observe the layout inflated in the constructor of ActivityChooserView, in this case R.layout.activity_chooser_view.
  4. Look up source of R.layout.activity_chooser_view and find the layout used for the button, in this case @+id/expand_activities_button.
  5. Observe the resource used for the background, in this case, android:background="?android:attr/actionBarItemBackground"
查看更多
叼着烟拽天下
5楼-- · 2019-05-11 21:02

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>
查看更多
登录 后发表回答