本文介绍了更改棒棒糖操作栏动作栏项目的pressed颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用AppCompat,并已成功地实施了推出了棒棒糖新的操作栏。唯一的问题是行动项目的pressed背景颜色。我想表现出不同的背景颜色操作项时pressed。任何想法如何可以做到?

I am using AppCompat and have successfully implemented the new action bar that rolled out with lollipop. The only problem is the pressed background color of the action items. I want to show a different background color for the action item when pressed. Any idea how it can be done?

推荐答案

您有几个选项。但首先,一些背景:

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

在AppCompat行动项目使用的主题属性的?ATTR / actionBarItemBackground (见RES /价值/ styles_bas​​e.xml),它被设置为?ATTR / selectableItemBackgroundBorderless (见水库/styles/themes_bas​​e.xml)默认情况下。这个属性被设置为一个无国界的纹波L和 @绘制/ abc_item_background_holo_light 在previous版本。

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.

操作栏本身是通过设置?ATTR / actionBarTheme (themes_bas​​e.xml)为主题,并设置为 @风格/ ThemeOverlay.AppCompat.ActionBar 默认情况下。在霍洛,这个主题将覆盖actionBarItemBackground,所以你需要在这里进行更改。

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.

所以,最简单的方式来覆盖行动项目背景,所有的动作条会在你的actionBarTheme设置actionBarItemBackground。你可能也想覆盖selectableItemBackground因为CloseMode项目不使用actionBarItemBackground(不知道为什么)。

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).

值/ styles.xml:

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>

注意:这些变化适用于所有的API层面,因此,如果您仍然希望在API 21+涟漪,你会想@可绘制/ whatever_you_want双方绘制和绘制-V21版本,后者其中采用了涟漪。

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.

这篇关于更改棒棒糖操作栏动作栏项目的pressed颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:38