简体   繁体   English

按下时更改Android ShareActionProvider背景颜色

[英]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. 操作栏中的ShareActionProvider有一种微调器,按下时会显示默认的全蓝色。

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) 我正在使用标准操作栏(没有兼容性或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 :( 关于样式ShareActionProvider的问题没有得到任何答案:(

I wanted white share button with a white background to popup. 我想要白色背景的白色分享按钮弹出。 So first I changed the textColorSecondary in my main theme to white. 首先,我将主要主题中的textColorSecondary更改为白色。

<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: 然后我使用了intips答案给出的弹出窗口的自定义样式:

    <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? ShareActionProvider使用标准的Action Bar背景吗? 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 . 有关如何构建XML Selector Drawable的示例,请查看系统使用的那个, 如下所示

Update 更新

How to find this yourself. 如何自己找到这个。

  1. Look up source of SharedActionProvider and examine onCreateActionView() . 查找SharedActionProvider源代码并检查onCreateActionView()
  2. Follow to ActivityChooserView which is the View created in onCreateActionView() . 关注ActivityChooserView ,它是在onCreateActionView()创建的View
  3. Observe the layout inflated in the constructor of ActivityChooserView , in this case R.layout.activity_chooser_view . 观察ActivityChooserView的构造函数中膨胀的布局,在本例中为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 . 查找R.layout.activity_chooser_view源代码并找到用于该按钮的布局,在本例中为@+id/expand_activities_button
  5. Observe the resource used for the background, in this case, android:background="?android:attr/actionBarItemBackground" 观察用于后台的资源,在这种情况下, android:background="?android:attr/actionBarItemBackground"

For anyone who has the same problem this is what worked for me: 对于有同样问题的人来说,这对我有用:

-in AppTheme add: -in AppTheme添加:

    <item name="listPopupWindowStyle">@style/PopupListStyle</item>

-create new style for listpopup: - 为listpopup创建新样式:

    <style name="PopupListStyle" parent="@style/Widget.AppCompat.Light.ListPopupWindow">
       <item name="android:popupBackground">@color/list_popup_bg</item>
    </style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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