This article describes an example of the use of ActionBar with fragment, ActionBar is a window function that identifies the application and user location, and provides the user with the operation and navigation modes.
The main uses of Action bar are:
1. Provide a dedicated space to identify the application's logo and the user's location.
The left side of this space is the app's icon or logo, as well as the activity's title. However, if it is a navigation label that identifies the current view object, such as the currently selected label, you can choose to delete the activity's title.
2. Provide a consistent navigation and visual experience between different applications.
The Action Bar provides built-in navigation labels for switching between fragment. It also provides a drop-down list that replaces the navigation mode or optimizes the current visual effects, such as a list sorted by different criteria.
3. Highlight activity's key operations (such as "search", "create", "share", etc.) and provide users with quick access within a predictable approach.
Realize:
Source:
Layout file:
Activity_main:
In the mainactivity layout, a fragment occupies all the activity layout space.
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:id= "@+id/main" tools:context= ". Mainactivity "></RelativeLayout>
File.xml:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " tools:context= ". Mainactivity "> <textview android:layout_width=" wrap_content " android:layout_height=" Wrap_ Content " android:layout_centerhorizontal=" true " android:text=" File Management interface " android:textsize=" 20SP "/ ></RelativeLayout>
Sys.xml:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " tools:context= ". Mainactivity "> <textview android:layout_width=" wrap_content " android:layout_height=" Wrap_ Content " android:layout_centerhorizontal=" true " android:text=" System Management Interface " android:textsize=" 20sp "/> </RelativeLayout>
Main.xml:
<menu xmlns:android= "Http://schemas.android.com/apk/res/android" > <item android:id= "@+id/sys" android:orderincategory= "android:showasaction=" "Always " android:title= "@string/sys"/> <item android:id= "@+id/file" android:orderincategory= "android:showasaction=" "Always" android:title= "@string/file"/> <item android:id= "@+id/product" android:orderincategory= " " android:showasaction=" always " android:title=" @string/product "/></menu>
Strings.xml:
<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name= "App_name" >fragmentdemo9_ menu</string> <string name= "action_settings" >Settings</string> <string name= "Hello_ World ">hello world!</string> <string name=" SYS "> System management </string> <string name=" File " > File Management </string> <string name= "Product" > Products Management </string></resources>
Code files:
Mainactivity:
Package Com.fragmentdemo9_menu;import Android.app.activity;import Android.app.fragmentmanager;import Android.app.fragmenttransaction;import Android.os.bundle;import Android.view.menu;import Android.view.MenuItem; Import android.widget.toast;/** * Actionbar with fragment Example Demo * */public class Mainactivity extends Activity {private F Ragmentmanager manager;private fragmenttransaction transaction; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); manager = Getfragmentmanager ();} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true;} @Overridepublic boolean onoptionsitemselected (MenuItem item) {transaction = Manager.begintransaction (); switch ( Item.getitemid ()) {/** * System Management */case r.id.sys:sysfragment sysfragment = new Sysfragment (); Transaction.replace (R.id.main , Sysfragment, "sysfragment"); Transaction.addtobackstack ("sysfragment"); Toast.maketext (This, "You clicked System Management", Toast.length_short). Show (); break;/** * File Management */case r.id.file:filefragment filefragment = new Filefragment () ; Transaction.replace (R.id.main, Filefragment, "filefragment"); Transaction.addtobackstack ("fileFragment"); Toast.maketext (This, "You clicked on File Management", Toast.length_short). Show (); break;/** * Product Management */case R.id.product:productfragment Productfragment = new Productfragment (); Transaction.replace (R.id.main, Productfragment, "productfragment"); Transaction.addtobackstack ("Productfragment"); Toast.maketext (This, "you clicked on product Management", Toast.length_short). Show (); break;} Transaction.commit (); return super.onoptionsitemselected (item);}}
Sysfragment:
Package Com.fragmentdemo9_menu;import Android.app.fragment;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.view;import android.view.viewgroup;/** * System Management related fragment * */public Class Sysfragment extends Fragment {@Overridepublic View oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View view = Inflater.inflate (R.layout.sys, null); return View;}}
Filefragment:
Package Com.fragmentdemo9_menu;import Android.app.fragment;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.view;import android.view.viewgroup;/** * File Management related fragment * */public Class Filefragment extends Fragment {@Overridepublic View oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View view = Inflater.inflate (r.layout.file, null); return View;}}
Productfragment:
Package Com.fragmentdemo9_menu;import Java.util.arraylist;import Android.app.listfragment;import android.os.Bundle ; Import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import android.widget.arrayadapter;/** * Product Management related fragment, showing a list of data. * */public class Productfragment extends Listfragment {private arrayadapter<string> adapter;private ArrayList< string> list; @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); list = New Arraylist<string> (); for (int i = 0; i <; i++) {List.add ("Rose" + i);} adapter = new Arrayadapter<string> (Getactivity (), Android. R.layout.simple_list_item_1, list); Setlistadapter (adapter);} @Overridepublic View Oncreateview (layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {return Super.oncreateview (Inflater, container, savedinstancestate);}}
Source code Download:
Click to download the source code