標籤:actionbar 相容 android
在Google官方Android設計指南中(連結:http://www.apkbus.com/design/get-started/ui-overview.html)有一個新特性就是自我標識,也就是宣傳自己,所以很多應用現在也自然的使用ActionBar並提供自己的logo.
的應用:
Google的Android設計指南中是這樣說的:應用的 啟動表徵圖 作為啟動應用的入口是展示 logo 的最佳場所。你也可以將啟動表徵圖放置在 操作欄 上,從而保證在應用內的所有頁面上都能看到它。
在使用ActionBar的時候,會發現一個問題。在3.0以前SDK中是不支援ActionBar的,所以如果手機apk要相容2.2或2.3的手機就需要用一個開源的項目ActionBarSherlock,具體使用方法如下:
1、下載開源包:http://actionbarsherlock.com/usage.html
2、匯入到Eclipse中(和匯入項目步驟相同,記得勾選Is Library)
3、在項目中引用(properties->android->add 加進去)
4、修改主題為@Style/Theme.Sherlock.Light(或其子類)
5、繼承SherlockActivity。
6、使用getSupportActionBar()擷取ActionBar對象。
上面方法就可以實現低版本使用ActionBar的問題,但是Goole去年推出了自己的相容包,使用起來更加方便。下面我們就來看看如何使用support_v7。
1、和上面一樣下載和匯入appcompat_7.x相容包(如果是官方最新的sdk開發工具則提供)
2、在項目中引用:
3、修改主題為@style/Theme.AppCompat(或其子類)
4、修改menu/目錄下對應的xml檔案
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:alpha="http://schemas.android.com/apk/res-auto"> <!-- Search, should appear as action button --> <item android:id="@+id/action_search" android:icon="@drawable/ic_action_refresh" android:title="重新整理" alpha:showAsAction="always"/> <!-- Settings, should always be in the overflow --> <item android:id="@+id/action_add" android:title="分享" android:icon="@drawable/ic_action_share" alpha:showAsAction="always" /> <item android:id="@+id/action_settings" android:title="更多" android:icon="@drawable/ic_action_overflow" alpha:showAsAction="always"> <menu > <group > <item android:id="@+id/item1" android:title="個人中心" android:icon="@drawable/ic_action_share"/> <item android:id="@+id/item2" android:title="設定" android:icon="@drawable/ic_action_share"/> <item android:id="@+id/exit_system" android:title="退出" android:icon="@drawable/ic_action_share"/> </group> </menu> </item> </menu>
5、繼承自ActionBarActivity
6、使用getSupportActionBar擷取ActionBar對象。
ActionBar actionBar = getSupportActionBar();actionBar.setDisplayShowHomeEnabled(true);actionBar.setIcon(R.drawable.actionbar_icon);
在Android 2.2和2.3手機上完美運行...