Change the pressed color of action bar item of Lollipop action bar

妖精的绣舞 提交于 2019-12-06 06:10:58

You have a couple of options. But first, some background:

The action items in AppCompat use theme attribute ?attr/actionBarItemBackground (see res/values/styles_base.xml) which is set to ?attr/selectableItemBackgroundBorderless (see res/styles/themes_base.xml) by default. This attribute is set to a borderless ripple on L and @drawable/abc_item_background_holo_light on previous versions.

The action bar itself is themed by setting ?attr/actionBarTheme (themes_base.xml) and is set to @style/ThemeOverlay.AppCompat.ActionBar by default. On Holo, this theme overrides the actionBarItemBackground, so you'll need to make your changes here.

So, the easy way to override the action item background for all action bars would be to set actionBarItemBackground in your actionBarTheme. You'll probably also want to override selectableItemBackground since the CloseMode item doesn't use actionBarItemBackground (no idea why).

values/styles.xml:

<style name="MyAppTheme" parent="Theme.AppCompat">
    ...
    <item name="actionBarTheme">@style/MyActionBarTheme</item>
</style>

<style name="MyActionBarTheme" parent="ThemeOverlay.AppCompat.ActionBar">
    ...
    <item name="actionBarItemBackground">@drawable/whatever_you_want</item>
    <item name="selectableItemBackground">@drawable/whatever_you_want</item>
</style>

Note: These changes apply to all API levels, so if you still want ripples on API 21+, you'll want both drawable and drawable-v21 versions of @drawable/whatever_you_want, the latter of which incorporates ripples.

for me this worked:

    <!-- pre 21 -->
    <item name="actionBarItemBackground">@drawable/selector_ab_tab_indicator</item>
    <!-- post 21 -->
    <item name="android:selectableItemBackgroundBorderless">@drawable/selector_ab_tab_indicator</item>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!