Android UI -- AppCompat Action Bar thirty-fifth


      Every Android developer design on Action Bar this is not strange, after all, it has released at least two years. The Android team releasedAction BarAt the same time released the design specificationsActionBarApi to support this design. If the ActionBar is not too familiar with the reference

Android UI development twenty-fourth——Action Bar . ActionBar API was added in the Android3.0 (API level 11), low version or not, does not fit the application supports Android 2.X series. I was fortunate enough to have third party open source actionbarsherlock support makes Android more than 2.1 of the Android application using Action Bar actionbarsherlock definition. Here we do not introduce how to use actionbarsherlock, we introduce an updated official supportAppCompat The Android2.1 version of the above can be achieved Action Bar.


   

Google I/O 2013 AppCompat Action Bar

      In the latest AppCompat API 18 Android Support Library. The use of AppCompat in library form into the application, AppCompat<sdk>/extras/android/support/v7/appcompat/ Position, need to download, or upgrade SDK.

      If the application is implemented using actionbarsherlock Action Bar, also do not have to deliberately change the AppCompat. Because actionbarsherlock is a very stable after many developers verification development library.

ActionBarSherlock is a solid and well-tested library which has served developers very well for a long time. If you are already using it and do not currently require any of the above then there is no need to migrate.

1)Import the AppCompat Library


The use of AppCompat the first step you need to import the AppCompat library, this step is not described in detail.

2)Modify the android:theme

Each Action Bar Activity should add Android:theme


<activity ... android:theme="@style/Theme.AppCompat" />


Or modify the application



<application android:label="@string/app_name" android:icon="@drawable/ic_launcher" android:theme="@style/Theme.AppCompat" android:allowBackup="true">

3)Activity to inherit from ActionBarActivity


      Implementation of Action Bar view need to inherit from ActionBarActivity.


4)Modify the menu namespace


<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:holo="http://schemas.android.com/apk/res-auto" > <item android:id="@+id/action_websearch" android:icon="@drawable/action_search" android:title="@string/action_websearch" holo:showAsAction="never"/> </menu>

Want the special attention is, through the XML file for Action Item, must be custom namespace, and the namespace showAsAction and item suffix to prefix consistent, in this case“holo”

Display Menu need to override the onCreateOptionsMenu method:


@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main, menu); return super.onCreateOptionsMenu(menu); }


The Menu item event processing need to override the onOptionsItemSelected method.



@Override public boolean onOptionsItemSelected(MenuItem item) { // The action bar home/up action should open or close the drawer. // ActionBarDrawerToggle will take care of this. if (mDrawerToggle.onOptionsItemSelected(item)) { return true; } // Handle action buttons switch(item.getItemId()) { case R.id.action_websearch: // create intent to perform web search for this planet Intent intent = new Intent(Intent.ACTION_WEB_SEARCH); intent.putExtra(SearchManager.QUERY, getSupportActionBar().getTitle()); // catch event that there's no activity to handle intent if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } else { Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show(); } return true; default: return super.onOptionsItemSelected(item); } }

It is simple to achieve through the Appcompat Action Bar, think of all the custom attributes refer to the official document.



Demo download: demo


/**
* @Author Zhang Xingye
*
* Introduction to iOS group: 83702688
* Android development of advanced group: 241395671
* I micro-blog Sina: @Zhang Xingye TBOW
*/


Reference resources:

http://antonioleiva.com/actionbarcompat-how-to-use/

http://antonioleiva.com/actionbarcompat-action-views/

http://android-developers.blogspot.com/2013/08/actionbarcompat-and-io-2013-app-source.html

Posted by Leo at November 19, 2013 - 11:59 PM