Overview:
In the honeycomb version of the system, Android introduced a new action bar. The action bar not only replaces the traditional title bar (title bar), which displays the title and app icon, but also provides additional features such as placing menu options, configuring app icons as navigation buttons, and more. --"The authoritative guide for Android Programming"
Advantages:
1. Easy to use, avoid the developers to repeat the production of wheels.
2. The framework is mature and does not require the developer to do the redundant adaptation of different resolution and fragmentation optimization operations.
Normal use (API14 and above):Code and Thinking Analysis:
Under normal circumstances, we write actionbar through the combination of Java code and XML code.
The XML file is as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><menu xmlns:android= "Http://schemas.android.com/apk/res/android" > <item android:id= "@+id/action_compose" android:icon= "@drawable/delete_button_normal" Android:showasaction= "Always" android:title= "@string/action_compose"/> <item android:id= "@+ Id/action_delete " android:icon=" @drawable/delete_button_normal " android:showasaction=" Always " android:title= "@string/action_delete"/> <item android:id= "@+id/action_settings" android: icon= "@drawable/delete_button_normal" android:showasaction= "always" android:title= "@string/action_ Settings "/></menu>
Define 3 Item, set its Showasaction property to always.
There are several values for the Showasaction property:
Always: This value causes the menu item to be displayed on the action Bar
Ifroom: If there is enough space, this value causes the menu item to appear on the action Bar
Never: This value causes the menu item to never appear on the action Bar
Withtext: This value causes the menu item and its icon to be displayed together with the menus text
See the above label as menu, you should know, to the above in the Java code is through the menu to achieve. The main method is Oncreateoptionsmenu, as follows:
public boolean Oncreateoptionsmenu (Menu menu) {menuinflater inflater = Getmenuinflater (); Inflater.inflate (R.menu.menu , menu); return true;}
In order to go in response to the left return key (arrow) and home need us to do the following logic in the code:
ActionBar bar = Getactionbar (); bar.sethomebuttonenabled (true); bar.setdisplayhomeasupenabled (false); Bar.setdisplayshowhomeenabled (TRUE);
Another point is the home icon, which we can set in the Activity tab of manifest, as follows:
<activity android:name= "com.actionbar.activity.MainActivity" android:label= "@string/action_bar_ Title " android:logo=" @drawable/back_button_normal "> <intent-filter> <action android: Name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity>
implementation results show:
Note: Sometimes we have a small arrow on the far left when the screen is big enough, so how do we get rid of the actionbar arrow on the left? You can click here to view the workaround.
suitable for 2.x version:Code and Thinking Analysis:
for the above logical method can only be implemented on the system of API 14 and above . So how does the system below API 14 achieve the same effect? Before the official introduction of a new solution, there were some great gods who began to study and developed a framework--actionbarsherlock. Of course, Google has now provided a new solution to this problem-Android-support-v7-appcompat. We can let our activity inherit from actionbaractivity, but don't worry about this actionbaractivity will have any bad effect on the code, because it is inherited from Fragmentactivity.
So how do we do it? We need to introduce a toolkit:Android-support-v7-appcompat, and import it into Android projects. Is it already done here? After running the program, we find that this is not the case. However, we did not find any clues on the base code, so how do we modify this bug? Google Bar, Kid.
Later, we found that we could no longer use static XML code to create it. We can dynamically generate the item from Java code in Java code dynamically, as follows:
public boolean Oncreateoptionsmenu (Menu menu) {//menuinflater inflater = Getmenuinflater ();//inflater.inflate ( R.menu.menu, menu);//return super.oncreateoptionsmenu (menu); Menucompat.setshowasaction (Menu.add ("first"). SetIcon (Android. R.drawable.btn_star), menuitemcompat.show_as_action_always); Menucompat.setshowasaction (Menu.add ("Second"). SetIcon (Android. r.drawable.ic_menu_rotate), menuitemcompat.show_as_action_always); return true;}
The commented out part is created statically through XML, which proves to be a failure and needs to be created dynamically.
Run Show:
I am poorly written and may have some statements that are not clear enough. More detailed code and details in the following source connection to download the view. It is also important to note that if your test machine is a Meizu family, there may not be a good fit in some places, please change the test machine to re-test or change the attribute parameter to Show_as_action_always (I use Meizu as well).
Source Download:
http://download.csdn.net/detail/u013761665/8737119
Android control Actionbar analysis and adaptation of 2.x method introduction