After two nights of research, we can finally achieve a simple actionbar. During this period, I found that most of the articles explaining Actionbar are based on more than 4.0 versions, for low version use Actionbar No detailed explanation, after a long time search found the following two relatively good articles:
Http://www.cnblogs.com/tianzhijiexian/p/3871416.html
http://blog.csdn.net/xyz_lmn/article/details/12623609
After my practice found to be in the 3.0 version of the implementation of Actionbar is really a struggle, the following is specific to the lower version of the steps to use Actionbar
1, first to add 4.0 support
(1) Project right------Android--- Existing android Code into workspace
--Select: \sdk\extras\android\support\v7\appcompat (according to your own file storage Path First choice)
-Tick copy projects into workspace--and finish
(If unable to resolve target ' android-16 ' appears, modify project.properties)
(2) referencing android.support.v7 in the project
Project right ---------------------
2, modify the system theme (1) androidmanifest.xml in the application node Android:theme= "@style/appbasetheme" property corresponding to the value modification Ctrl + LEFT mouse button into the values/ Styles.xml modification
<style name= "Appbasetheme" parent= "Theme.AppCompat.Light" >
(2) At the same time values11/styles.xml corresponding subject also to be modified
<style name= "Appbasetheme" parent= "Theme.AppCompat.Light" >
The first one is for 2.3.3 and the following versions, the second one is for 4.0 and above.
The following operations are basically the same as http://www.cnblogs.com/tianzhijiexian/p/3871416.html.
3. Add MenuItem to Actionbar
The new/res/menu/menuitem.xml content is as follows:
<menu xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:app= "http://schemas.android.com/ Apk/res-auto " xmlns:tools=" Http://schemas.android.com/tools " tools:context=" Com.liuc.ui.activity.MainActivity > <!--Custom View menu only shows icons-- <item android:id= "@+id/clock_ item_id " app:actionlayout=" @layout/actionbar_useritem " app:showasaction=" always "/> </menu>
The content of the corresponding Res/layout/actionbar_item.xml is
<?xml version= "1.0" encoding= "Utf-8"? ><imageview xmlns:android= "http://schemas.android.com/apk/res/ Android " android:padding=" 5DP " android:layout_width=" wrap_content " android:layout_height=" Wrap_ Content " android:src=" @drawable/header " ></ImageView>
Here's one thing to note:
* The official recommendation is to have the icon and title icons for each item item, as the system shows the icon by default,
* And when there is not enough screen space, the menu that is not displayed is hidden in the list, and only the value of the Title field is displayed in the list.
<item
Android:id= "@+id/clock_item_id"
android:showasaction= "Withtext"
android:title= "Clock"
app:actionlayout= "@layout/clock"
app:showasaction= "Always"/>
It is important to note that the file has its own namespace. And you need to use the properties in the custom namespace when setting showasaction or actionlayout, otherwise it's not valid!
The custom namespace statement here is:
xmlns:app= "Http://schemas.android.com/apk/res-auto"
The code for the Activity load menu is:
Package Com.liuc.ui.activity;import Android.os.bundle;import Android.support.v7.app.actionbar;import Android.support.v7.app.actionbaractivity;import Android.view.menu;import Android.view.menuinflater;import com.liuc.r;/** * Constellation Program Entry * @author Liuchao * */public class Mainactivity extends actionbaractivity{private ActionBar action bar;protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); ActionBar = Getsupportactionbar ();//Initialize ActionBar actionbar.setdisplayhomeasupenabled (true);//Show Left Small arrow action Bar.setdisplayshowhomeenabled (TRUE);//Show home image//Actionbar.seticon (Getresources (). Getdrawable (R.drawable.ic_ launcher));//Set the left icon Actionbar.setbackgrounddrawable (Getresources (). getdrawable (R.drawable.bar_color));//Set background image} @Override public boolean Oncreateoptionsmenu (Menu menu) {menuinflater inflater = Getmenuinflater (); Inflater.inflate (r.menu.mainmenu, menu); return Super.oncreateoptionsmenu (menu); } }
Such a simple actionbar effect is done.
Code Address: http://download.csdn.net/detail/shanhuhau/7954799
As follows:
Android project DEVELOPMENT (1) continued--The top menu of the homepage is implemented with Actionbar