Modifying style does not work well

2019-07-15 10:15发布

问题:

I'm changing the UI of my app to green colors. In the ActionBar, I use a ShareActionProvider to share data to other users, but this menu item doesn't get the same color as the other menu items. However, its dropdown listview has the correct colors.

I'm running my app in GingerBread, so when I press menu key, a menu item appears but it also has the wrong colors.

I have used Android Action Bar Style Generator, but there is something I'm missing. This is my code in /res/values:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="actionBarItemBackground">@drawable/selectable_background_newtheme</item>
    <item name="actionBarStyle">@style/ActionBar.Solid.NewTheme</item>
    <item name="popupMenuStyle">@style/PopupMenu.NewTheme</item>
    <item name="dropDownListViewStyle">@style/DropDownListView.NewTheme</item>
    <item name="actionDropDownStyle">@style/DropDownNav.NewTheme</item>

    <!-- Light.DarkActionBar specific -->
    <item name="actionBarWidgetTheme">@style/NewTheme.Widget</item>
</style>



<!-- ActionBar style -->
<style name="ActionBar.Solid.NewTheme" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="background">@drawable/action_bar_gradient</item>
</style>

<style name="PopupMenu.NewTheme" parent="@style/Widget.AppCompat.PopupMenu">
    <item name="android:popupBackground">@drawable/background_gradient</item>
</style>

<style name="DropDownListView.NewTheme" parent="@style/Widget.AppCompat.ListView.DropDown">
    <item name="android:listSelector">@drawable/selectable_background_newtheme</item>
</style>

<style name="DropDownNav.NewTheme" parent="@style/Widget.AppCompat.Spinner.DropDown.ActionBar">
    <item name="android:background">@drawable/spinner_background_ab_newtheme</item>
    <item name="android:dropDownSelector">@drawable/selectable_background_newtheme</item>
</style>

<!-- this style is only referenced in a Light.DarkActionBar based theme -->
<style name="NewTheme.Widget" parent="@style/Theme.AppCompat">
    <item name="popupMenuStyle">@style/PopupMenu.NewTheme</item>
    <item name="dropDownListViewStyle">@style/DropDownListView.NewTheme</item>
</style>

Is there any missing item? Thanks in advance

回答1:

I finally got it working myself. I had to add:

<item name="android:listChoiceBackgroundIndicator">@drawable/selectable_background_realestate</item>

in *themes_base.xml* in values-v14 and

<item name="listChoiceBackgroundIndicator">@drawable/selectable_background_realestate</item>

in themes_base.xml in values for the correct highlight color when pressing a list item.

I also had to change actionBarItemBackground in themes_base.xml (values and values-v14) to reference my drawable so that the share icon would get the same highlight.

Finally to get the right color of the dropdown menu item I had to change android:popupBackground to @drawable/menu_dropdown_panel_realestate in styles_base.xml (values and values-v14) for Widget.AppCompat.Base.ListPopupWindow

The drawables I referenced in my answer were generated using Android Asset Studio for AppCompat



回答2:

I think I can help you solve your Gingerbread problem. That menu is called a "panel" in the AppCompat style & theme .XML files.

So to override it with your own selector colour, this is what I suggest:

In your custom theme, CustomActionBarTheme, add this line:

    <item name="panelMenuListTheme">@style/Theme.NewTheme.CompactMenu</item>

And add these two styles:

<style name="Theme.NewTheme.CompactMenu" parent="@style/Theme.AppCompat.CompactMenu">
     <item name="android:listViewStyle">@style/myCustomMenuStyle</item>
</style>

and

<style name="myCustomMenuStyle" parent="@style/Widget.AppCompat.ListView.Menu">
    <item name="android:listSelector">@drawable/newtheme_list_selector_holo_light</item>
</style> 

note: the @drawable/newtheme_list_selector_holo_light is an xml resource that I got from the Android Holo Colors Generator.

I suggest you use the website, by the way, you only need the resources of the "List Selector" in this case, and grab the following resources from the ZIP file that it outputs, and put them in your drawable folders.

Specifically:

newtheme_list_selector_holo_light.xml > in res/drawable

newtheme_list_selector_background_transition_holo_light.xml > in res/drawable

And the mdpi, hdpi, xdpi and xxdpi of the following .9.png resources:

newtheme_list_selector_disabled_holo_light.9.png

newtheme_list_pressed_holo_light.9.png (similar to the png Action Bar Generator created)

newtheme_list_longpressed_holo.9.png

newtheme_list_focused_holo.9.png (similar to the png Action Bar Generator created)



回答3:

A bit later however it might help. In GingerBread, when you press menu key, the menu item which appears at the bottom can be customize by the following attribute itemBackground:

<style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="android:itemBackground">@drawable/selectable_background_newtheme</item>
</style>  

Then, apparently to customize the ShareActionProvider item pressed, you need this attribute:

<style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="android:selectableItemBackground">@drawable/selectable_background_newtheme</item>
</style>