Change Action Bar onPressed color

后端 未结 2 1496
不知归路
不知归路 2020-12-15 07:09

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.

相关标签:
2条回答
  • 2020-12-15 07:49

    Fixed it. There was a parent activity changing the theme in the onCreate method...

    I didn't know that you could set the theme programmatically within the activity with:

    setTheme(R.style.MyTheme);
    
    0 讨论(0)
  • 2020-12-15 08:07

    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>
    
    0 讨论(0)
提交回复
热议问题