This article transferred from: http://blog.csdn.net/u010933209/article/details/40112079
Problem One: Icon does not display properly
has always been to actionbar love and hate, especially when the first contact when there is a very headache problem, is clearly showasaction not set as never, how is not displayed?
Later, the eclipse-generated menu and its own menu file were compared, and the menu generated by Eclipse was found to have multiple namespaces xmlns:app= "Http://schemas.android.com/apk/res-auto", and the Eclipse Auto-generated icon prefix is App:icon, and then the icon prefix of its own definition of item is also changed to the app, and sure enough, it will be displayed correctly.
The following is the official Google Android training tutorial to see:
If you use the Support library for versions compatible with Android 2.1, android showAsAction properties are not available under namespaces. The Support library provides properties that override it, and you must declare your own XML namespace and use that namespace as the property prefix. (A custom XML namespace needs to be based on your app name, but you can take whatever name you want, its scope is only within the file you declare.) For example
Res/menu/main_activity_actions.xml
<Menuxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:yourapp= "Http://schemas.android.com/apk/res-auto" > <!--Search, should appear as action button - <ItemAndroid:id= "@+id/action_search"Android:icon= "@drawable/ic_action_search"Android:title= "@string/action_search"yourapp:showasaction= "Ifroom" /> ... </Menu>
In other words, if you use the android.support.v7 package, the icon property prefix must be a custom namespace prefix, and icon will display normally.
Issue two: overflow cannot be displayed
For beginners is a more crazy problem. It's hard to come up with a scratching head.
Later on-line to find information to find that the original is related to mobile phone hardware. If your phone already has a physical menu key, overflow will not be displayed. If you want to display it, you can only control it through code.
private void Setoverflowshowingalways () { try {viewconfiguration config = viewconfiguration.get (this ); Field Menukeyfield = viewconfiguration. class . Getdeclaredfield ("Shaspermanentmenukey" ); Menukeyfield.setaccessible ( true ); Menukeyfield.setboolean (config, false ); catch (Exception e) {E.printstacktra CE (); }
the method is then called in the OnCreate method
Yes, the default setting of overflow is changed by reflection, so that it can be displayed regardless of the physical menu key.
Had to spit the design of this egg-ache.
First contact with Android Actionbar more annoying problems [turn]