Android UI開發——AppCompat實現Action Bar

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是一個很穩定的經過很多開發者驗證的開發庫。

  1. ActionBarSherlock is a solid and well-tested library which has served developers very well for a long time.   
  2. 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

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

或者修改application

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

3)Activity要繼承自ActionBarActivity

        實現Action Bar的視圖需要繼承ActionBarActivity。


4)修改menu的命名空間

  1. <menu xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:holo="http://schemas.android.com/apk/res-auto" >  
  3.   
  4.     <item  
  5.         android:id="@+id/action_websearch"  
  6.         android:icon="@drawable/action_search"  
  7.         android:title="@string/action_websearch"  
  8.         holo:showAsAction="never"/>  
  9. </menu>  

      要特別注意的是,通過XML文件來實現Action Item,一定要自定義命名空間,而且該命名空間的後綴一定要和item中showAsAction的前綴一致,本例中爲“holo”

顯示Menu需要重寫onCreateOptionsMenu方法:

  1. @Override  
  2.    public boolean onCreateOptionsMenu(Menu menu) {  
  3.        MenuInflater inflater = getMenuInflater();  
  4.        inflater.inflate(R.menu.main, menu);  
  5.        return super.onCreateOptionsMenu(menu);  
  6.    }  

對Menu的item事件處理需要重寫onOptionsItemSelected方法。

  1. @Override  
  2.     public boolean onOptionsItemSelected(MenuItem item) {  
  3.          // The action bar home/up action should open or close the drawer.  
  4.          // ActionBarDrawerToggle will take care of this.  
  5.         if (mDrawerToggle.onOptionsItemSelected(item)) {  
  6.             return true;  
  7.         }  
  8.         // Handle action buttons  
  9.         switch(item.getItemId()) {  
  10.         case R.id.action_websearch:  
  11.             // create intent to perform web search for this planet  
  12.             Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);  
  13.             intent.putExtra(SearchManager.QUERY, getSupportActionBar().getTitle());  
  14.             // catch event that there's no activity to handle intent  
  15.             if (intent.resolveActivity(getPackageManager()) != null) {  
  16.                 startActivity(intent);  
  17.             } else {  
  18.                 Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show();  
  19.             }  
  20.             return true;  
  21.         default:  
  22.             return super.onOptionsItemSelected(item);  
  23.         }  
  24.     }  


上面就是簡單的通過Appcompat實現Action Bar,想自定義各種屬性請參考官方文檔。


demo下載:demo

/**
* @author 張興業
*  iOS入門羣:83702688
*  android開發進階羣:241395671
*  我的新浪微博:@張興業TBOW
*/

參考:

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章