当按下时,改变actionbar按钮的背景色。

时间:2022-09-25 16:17:42

If I press a button in the action bar, then its background color is not what I want. The background color of my item doesn't respond to my click event. How can I change this and change the background color when it's pressed?

如果我在操作栏中按下一个按钮,那么它的背景颜色不是我想要的。我的项目的背景颜色没有响应我的点击事件。我如何改变这个和改变背景颜色,当它被按下?

4 个解决方案

#1


20  

You need to declare android:actionBarItemBackground attribute which is a:

您需要声明android:actionBarItemBackground属性为:

Custom item state list drawable background for action bar items.

自定义项目状态列表动作栏项目的可绘制背景。

Then, in your styles do as follows:

那么,你的风格如下:

<style name="CustomStyle" parent="@style/Theme.Holo.Light" >
    <item name="android:actionBarItemBackground">@drawable/ab_item_background</item>
    <item name="actionBarItemBackground">@drawable/ab_item_background</item>
</style>  

So, put your own drawable with a selector and every state (pressed, focused, disabled, etc) to have the expected background. For example, the drawable ab_item_background.xml declared above might be like this:

因此,将您自己的drawable与一个选择器以及每个状态(按下、焦点、禁用等)都具有预期的背景。例如,可绘制的ab_item_background。上面声明的xml可能是这样的:

<selector xmlns:android="http://schemas.android.com/apk/res/android" 
    android:exitFadeDuration="@android:integer/config_mediumAnimTime">
    <!-- focused/pressed: color=red -->
    <item 
        android:state_focused="true"
        android:state_pressed="true"
        android:drawable="@color/red" />
    <!-- pressed: color=red -->
    <item 
        android:state_pressed="true"
        android:drawable="@color/red" />
    <!-- normal: color=transparent -->
    <item 
        android:drawable="@android:color/transparent" />
</selector>

In Styling the Action Bar, you can find all the customization possibles and all the attributes to do so.

在对动作栏进行样式化时,您可以找到所有定制的可能性和所有这样做的属性。

#2


5  

i think you need this link too:

我想你也需要这个链接:

http://jgilfelt.github.io/android-actionbarstylegenerator/

http://jgilfelt.github.io/android-actionbarstylegenerator/

#3


2  

You got to customize you actionbar. Check out this two links

你需要自定义actionbar。看看这两个链接

Customizing ActionBar

定制ActionBar

Custom ActionBar

自定义ActionBar

#4


1  

ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0a0a0a")));

this might help

这可能帮助

#1


20  

You need to declare android:actionBarItemBackground attribute which is a:

您需要声明android:actionBarItemBackground属性为:

Custom item state list drawable background for action bar items.

自定义项目状态列表动作栏项目的可绘制背景。

Then, in your styles do as follows:

那么,你的风格如下:

<style name="CustomStyle" parent="@style/Theme.Holo.Light" >
    <item name="android:actionBarItemBackground">@drawable/ab_item_background</item>
    <item name="actionBarItemBackground">@drawable/ab_item_background</item>
</style>  

So, put your own drawable with a selector and every state (pressed, focused, disabled, etc) to have the expected background. For example, the drawable ab_item_background.xml declared above might be like this:

因此,将您自己的drawable与一个选择器以及每个状态(按下、焦点、禁用等)都具有预期的背景。例如,可绘制的ab_item_background。上面声明的xml可能是这样的:

<selector xmlns:android="http://schemas.android.com/apk/res/android" 
    android:exitFadeDuration="@android:integer/config_mediumAnimTime">
    <!-- focused/pressed: color=red -->
    <item 
        android:state_focused="true"
        android:state_pressed="true"
        android:drawable="@color/red" />
    <!-- pressed: color=red -->
    <item 
        android:state_pressed="true"
        android:drawable="@color/red" />
    <!-- normal: color=transparent -->
    <item 
        android:drawable="@android:color/transparent" />
</selector>

In Styling the Action Bar, you can find all the customization possibles and all the attributes to do so.

在对动作栏进行样式化时,您可以找到所有定制的可能性和所有这样做的属性。

#2


5  

i think you need this link too:

我想你也需要这个链接:

http://jgilfelt.github.io/android-actionbarstylegenerator/

http://jgilfelt.github.io/android-actionbarstylegenerator/

#3


2  

You got to customize you actionbar. Check out this two links

你需要自定义actionbar。看看这两个链接

Customizing ActionBar

定制ActionBar

Custom ActionBar

自定义ActionBar

#4


1  

ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0a0a0a")));

this might help

这可能帮助