Android UI -- AppCompat Action Bar thirty-fifth

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

      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="" xmlns:holo="" > <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(, 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 // 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:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

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