简体   繁体   中英

Change Android ShareActionProvider background color when pressed

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

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

在此输入图像描述

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

Doesn't the ShareActionProvider use the standard Action Bar background? If so, you can control this by overriding the resource used in your theme. Eg

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM