问题
I'm having real difficulty doing anything to the overflow menu in actionbar sherlock.
Ideally, I would like to use a custom TextView
for each item in order to set a different font on it and change the colour of the pressed
state.
I have tried (all without success):
Changing The Style Of Actionbar Overflow
Actionbar styled overflow menu items
listview as action overflow in sherlock actionbar
https://groups.google.com/forum/#!msg/actionbarsherlock/5lHOKNlXn_4/f9XicMXbFFAJ
My app will have different fragments, all extending BaseFragment
with different items in the overflow menu. I'm also using the v4 support package. I'm creating my menu like this:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.activity_borrow, menu);
}
activity_borrow.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_filter"
android:title="test"
android:orderInCategory="100"
android:showAsAction="never"
/>
</menu>
My app uses a theme that inherits from Theme.Sherlock
.
How can I used a custom view inside that menu? Or at the very least, how can I change the default blue pressed
state?
回答1:
To change the colors of the overflow list items, add two items to your application theme which is usually defined in res/values/styles.xml
:
<item name="android:dropDownListViewStyle">@style/DropDownListView</item>
<item name="dropDownListViewStyle">@style/DropDownListView</item>
In that same file, add the style that you have just assigned:
<style name="DropDownListView" parent="@style/Widget.Sherlock.ListView.DropDown">
<item name="android:listSelector">@drawable/selectable_background</item>
</style>
And finally create a selector drawable selectable_background.xml
in the drawable folder, containing this:
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="@android:integer/config_mediumAnimTime" >
<item android:state_pressed="false" android:state_focused="true" android:drawable="@color/focussed_color" />
<item android:state_pressed="true" android:drawable="@color/pressed_color" />
<item android:drawable="@android:color/transparent" />
</selector>
Finally, define the colors which are usually placed in colors.xml
:
<resources>
<color name="pressed_color">#FF8E4067</color> <!-- your purple tone already ;) -->
<color name="focussed_color">#DD8E4067</color>
</resources>
In my app, I used the "ActionBar Style Generator" as baboo suggested, which handles everything for you conveniently. For this answer, I just extracted and simplified the parts that I think make up the overflow menu styling.
I think there is some mystery about the effects of styling three different items:
- From my understanding,
android:dropDownListViewStyle
includes the overflow menu that hides behind the "three dots" in the ActionBar. - Not to be confused with
android:actionDropDownStyle
, which is used to the style the app navigation dropdown in case you usedactionBar.setNavigationMode(NAVIGATION_MODE_LIST)
- However, some Android devices with a hardware menu button (e.g. the Nexus S or Galaxy S3 Mini)
don't display the "three dots" but an overlay menu that slides in from
the bottom of the screen if the hardware menu button is clicked.
android:popupMenuStyle
is the correct attribute to style this.
Again, this is only as far as I can remember from my own app development.
Also, make sure to check that no other style files (e.g. folders with configuration qualifiers) overwrite the styles that you have just defined.
All in all, I understand that this would only change the background color of the list items. To use a completely custom view in there, you might create a custom spinner view, add a dummy button with a "three dots" icon to your ActionBar and open the spinner on click.
来源:https://stackoverflow.com/questions/15338033/using-a-custom-view-for-overflow-menu-items