如何单击时,使操作栏图标的变化 [英] How to make action bar icon change when clicked

查看:90
本文介绍了如何单击时,使操作栏图标的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SherlockActionBar一套紫色背景白色图标

I have a set of white icons on purple background in the SherlockActionBar

我希望让他们变成暗紫色当被点击。

I want to have them turn dark purple when being clicked on.

我在暗紫色的颜色相同的图标,所以我要让那些可绘制出现在pressed状态。

I have the same icons in dark purple color, so I want to make those drawables appear on pressed state.

现在,我知道如何做到这一点,在整个应用程序的主题,但它意味着我必须使用相同的绘制所有的图标。

Now, I know how to do this in the whole application theme, but it means that I have to use the same drawable for all the icons.

我想知道我怎么可以分配不同的绘制每个项目的pressed状态。

I want to know how can I assign different drawable for each item's pressed state.

下面是我用现在的code:

Here is the code that I use right now:

在styles.xml

In styles.xml

<style name="Theme.SherlockCustom" parent="@style/Theme.Sherlock.Light">
    <item name="actionBarItemBackground">@drawable/action_bar_item_background</item>
    <item name="android:icon">@android:color/transparent</item>
    <item name="displayOptions">showCustom</item>
    <item name="android:minWidth">0dp</item>
    <item name="android:padding">0dp</item>
</style>

action_bar_item_background.xml:

action_bar_item_background.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/light_purple" android:state_pressed="true"/>
 <!-- pressed -->
    <item android:drawable="@android:color/transparent"/>
 <!-- default -->

</selector>

这是不工作的一部分(试图设置定义了一个新的面貌为特定的按钮样式):

feed_icon.xml:

feed_icon.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:drawable="@drawable/ic_action_content_paste"
        android:right="@dimen/action_bar_icons_padding_right"
        android:theme="@style/feediconstyle"></item>


</layer-list>

和款式/ feediconstyle:

and style/ feediconstyle:

<style name="feediconstyle" parent="@style/Theme.Sherlock.Light">
    <item name="actionBarItemBackground">@drawable/feed_icon_background</item>
    <item name="displayOptions">showCustom</item>
</style>

这个特殊的图标不服从新的风格,这是

this particular icon does not obey to the new style, which is

feed_icon_background:

feed_icon_background:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/feed_icon_purple" android:state_pressed="true"/>
 <!-- pressed -->
    <item android:drawable="@android:color/transparent"/>
 <!-- default -->

</selector>

那么,如何做呢?

So how do I do it?

推荐答案

我想有资格以我没有检查这个只用ActionBarSherlock的标准动作条

I'd like to qualify with "I have not checked this using ActionBarSherlock only the standard actionbar"

不过我知道这会工作的标准动作条,并认为没有理由为什么它会不会在ActionBarSherlock。

That said I know this will work on the standard actionbar and see no reason why it would not in ActionBarSherlock.

在活动我的OnCreate(我使用的标识属性,但它会为的MenuItems工作以及)

In the onCreate of the activity I have (I'm using the logo attribute but it will work for MenuItems as well)

    getActionBar().setLogo(R.drawable.logo);
    getActionBar().setDisplayUseLogoEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

在logo.xml我有一个选择器

In logo.xml I have a selector

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" 
          android:drawable="@drawable/logopressed"/>
    <item android:drawable="@drawable/logonotpressed" />
</selector>

在标志pressed.xml图层列表可以用来做背景

In logopressed.xml A layer list can be used to make the background

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@android:color/holo_purple" />
    <item
        android:drawable="@drawable/logo_normal" />   
</layer-list>

在logonot pressed.xml根据需要切换出背景的颜色和/或图标可拉伸

In logonotpressed.xml switch out the background colour and/or icon drawable as desired

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@android:color/white" />
    <item
        android:drawable="@drawable/logo_inverted" />   
</layer-list>

所以,这个解决方案的关键是设置选择绘制,让它选择每个状态的全新绘制的,而不是一个选择绘制设置为,否则静态变量的背景属性

So the crux of this solution is to set the selector drawable and let it select a whole new drawable for each state rather than setting a selector drawable to the background attribute of a otherwise static variable

请注意:你应该能够主题与背景,而不是做2层的项目,但我一直没能得到这个工作

Note: you should be able to theme the items with a background instead of doing 2 layers but I haven't been able to get this to work

这篇关于如何单击时,使操作栏图标的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆