Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

android - Change Action Bar onPressed color

I cannot figure out how to change the action bar items "onPressed" color on Android. I'm not talking about the action bar background color but about the blue over state. How can I change the color of this or at least get rid of it? For instance the App Icon with the homeAsUpIndicator.

I tried to change the action bar style and also the menu item style but nothing worked.

Thanks

EDIT Here is my implementation so far:

My style xml:

<style name="Theme.AppTheme" parent="@android:style/Theme.Holo">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="android:selectableItemBackground">@android:color/transparent</item>
    <item name="android:windowEnterAnimation">@anim/fadein</item>
    <item name="android:windowExitAnimation">@anim/fadeout</item>        
    <item name="android:actionBarItemBackground">@android:color/transparent</item>
</style>

<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">@drawable/actionbarheader</item>
    <item name="android:backgroundStacked">@drawable/actionbarheader</item>
    <item name="android:backgroundSplit">@drawable/actionbarheader</item>
    <item name="android:titleTextStyle">@style/ActionBarTitle</item>
    <item name="android:actionBarItemBackground">@android:color/transparent</item>
    <item name="android:actionButtonStyle">@style/MyActionButtonStyle</item>
    <item name="android:homeAsUpIndicator">@drawable/actionbarlogo</item>
    <item name="android:icon">@drawable/app_icon</item>>
</style>

<style name="ActionBarTitle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textSize">20sp</item>
</style>

<style name="MyActionButtonStyle" parent="@android:style/Widget.Holo.Light.ActionButton">
    <item name="android:background">@android:color/transparent</item>
</style>

Then I have a drawable for the actionbarogo:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/actionbarlogo_on" android:state_pressed="true"/>
  <item android:drawable="@drawable/actionbarlogo_on" android:state_focused="true"/>
  <item android:drawable="@drawable/actionbarlogo_off"/>
</selector>

My menu xml for the action bar items:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/ContactsMode"
        android:icon="@drawable/actionbarcontacts"
        android:title="@string/contacts"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:background="@android:color/transparent" />
</menu>

and finally, the item's drawable:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/actionbarcontacts_on" android:state_pressed="true"/>
  <item android:drawable="@drawable/actionbarcontacts_on" android:state_focused="true"/>
  <item android:drawable="@drawable/actionbarcontacts_off"/>
</selector>

Here is how I create the Menu Items for the ActionBar:

public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        actionBar.setIcon(R.drawable.actionbarlogo);
        return true;
    }

    public boolean onPrepareOptionsMenu(Menu menu) {        
         MenuItem item= menu.findItem(R.id.ContactsMode);
         if(ContactsButton)
             item.setVisible(true);      
         else
            item.setVisible(false);

         return true;   
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To set the background for app icon together with the homeAsUpIndicator (there is a common background for these two icons) you have to set android:actionBarItemBackground item in the theme. The theme has to contain something like this (I assume that you use ActionBarSherlock):

<style name="MyTheme" parent="@style/Theme.Sherlock">
    <!-- for ActionBarSherlock -->
    <item name="actionBarItemBackground">@drawable/my_background</item>
    <!-- for native ActionBar -->
    <item name="android:actionBarItemBackground">@drawable/my_background</item>
</style>

Where the drawable drawable/my_background.xml would be a StaleListDrawable, containing something like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_pressed="true"
        android:drawable="@drawable/my_background_pressed" />
    <item
        android:drawable="@drawable/my_background_normal" />
</selector>

To disable the background completely, you can set a transparent background, or value @null in the theme might also work.

The theme item android:actionBarItemBackground sets the background for app icon and homeAsUpIndicator. But it also sets the default background for menu items, overflow icon and background for title. These backgrounds can be overridden.

Menu items

To change the menu item background set a proper (android:)actionButtonStyle in the theme like this:

<style name="MyTheme" parent="@style/Theme.Sherlock">
    <item name="actionButtonStyle">@style/MyActionButtonStyle</item>
    <item name="android:actionButtonStyle">@style/MyActionButtonStyle</item>
</style>

And MyActionButtonStyle will contain something like this:

<style name="MyActionButtonStyle" parent="@style/Widget.Sherlock.ActionButton">
    <item name="android:background">@drawable/my_background</item>
</style>

Overflow icon

And finally, to change the overflow icon background set a properandroid:actionOverflowButtonStyle in the theme like this:

<style name="MyTheme" parent="@style/Theme.Sherlock">
    <item name="actionOverflowButtonStyle">@style/MyOverflowButtonStyle</item>
    <item name="android:actionOverflowButtonStyle">@style/MyOverflowButtonStyle</item>
</style>

And MyOverflowButtonStyle will contain something like this:

<style name="MyOverflowButtonStyle" parent="@style/Widget.Sherlock.ActionButton.Overflow">
    <item name="android:background">@drawable/my_background</item>
</style>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...