http://blog.csdn.net/xyz_lmn/article/details/12623609
每一位Android開發者對Action Bar這種設計都不陌生了,畢竟它已經發布了至少兩年了。Android團隊發佈Action Bar設計規範時同時放出了ActionBar的Api來支持這種設計。如果對ActionBar不太熟悉的可以參考《
Android UI開發第二十四篇——Action Bar》。ActionBar的API被添加在Android3.0(API 級別 11)中,低版本的還是用不了,根本不能適配支持Android 2.X系列的應用。很幸運有第三方開源的actionbarsherlock支持使得Android 2.1以上的Android應用使用actionbarsherlock定義的Action Bar。這裏我們不介紹actionbarsherlock怎麼使用,我們介紹一種更新的官方支持的AppCompat 使得Android2.1以上的版本可以實現Action Bar。
Google I/O 2013中AppCompat實現的Action Bar效果
AppCompat在最新的API 18的Android Support Library中。使用AppCompat需要以庫的形式引入到應用中,AppCompat在<sdk>/extras/android/support/v7/appcompat/
的位置,需要自行下載,或者升級SDK。
如果應用是使用actionbarsherlock實現的Action Bar,也不必刻意的改成AppCompat。因爲actionbarsherlock是一個很穩定的經過很多開發者驗證的開發庫。
- 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)導入AppCompat庫
使用AppCompat第一步需要導入AppCompat庫,這一步就不做詳細介紹了。
2)修改 android:theme
每個使用Action Bar的Activity都應該添加Android:theme
- <activity
- ...
- android:theme="@style/Theme.AppCompat" />
或者修改application
- <application
- android:label="@string/app_name"
- android:icon="@drawable/ic_launcher"
- android:theme="@style/Theme.AppCompat"
- android:allowBackup="true">
3)Activity要繼承自ActionBarActivity
實現Action Bar的視圖需要繼承ActionBarActivity。
4)修改menu的命名空間
- <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>
要特別注意的是,通過XML文件來實現Action Item,一定要自定義命名空間,而且該命名空間的後綴一定要和item中showAsAction的前綴一致,本例中爲“holo”
顯示Menu需要重寫onCreateOptionsMenu方法:
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- MenuInflater inflater = getMenuInflater();
- inflater.inflate(R.menu.main, menu);
- return super.onCreateOptionsMenu(menu);
- }
對Menu的item事件處理需要重寫onOptionsItemSelected方法。
- @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);
- }
- }
上面就是簡單的通過Appcompat實現Action Bar,想自定義各種屬性請參考官方文檔。
demo下載:demo
參考:
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