APP Bar1,應用欄或者操作欄,使用應用欄可讓您的應用與其他 Android 應用保持一致,允許用戶快速了解如何使用您的應用并獲得一流的體驗。應用欄的主要功能包括:
一個專用區域,可以標識您的應用并指示用戶在應用中的位置;以可預測的方式訪問搜索等重要操作;支持導航和視圖切換(通過標簽頁或下拉列表);Beginning with Android 3.0 (API level 11), all activities that use the default theme have an ActionBar as an app bar. However, app bar features have gradually been added to the native ActionBar over various Android releases. As a result, the native ActionBar behaves differently depending on what version of the Android system a device may be using. By contrast, the most recent features are added to the support library’s version of Toolbar, and they are available on any device that can use the support library.
For this reason, you should use the support library’s Toolbar class to implement your activities’ app bars. Using the support library’s toolbar helps ensure that your app will have consistent behavior across the widest range of devices. For example, the Toolbar widget PRovides a material design experience on devices running Android 2.1 (API level 7) or later, but the native action bar doesn’t support material design unless the device is running Android 5.0 (API level 21) or later.
從 Android 3.0(API 級別 11)開始,所有使用默認主題的 Activity 均使用 ActionBar 作為應用欄。不過,經過不同 Android 版本的演化,應用欄功能已逐漸添加到原生 ActionBar 中。因此,原生 ActionBar 的行為會隨設備使用的 Android 系統的版本而發生變化。相比之下,最新功能已添加到支持庫版本的 Toolbar 中,并且這些功能可以在任何能夠使用該支持庫的設備上使用。
因此,您應使用支持庫的 Toolbar 類來實現 Activity 的應用欄。使用支持庫的工具欄有助于確保您的應用在最大范圍的設備上保持一致的行為。例如,Toolbar 小部件能夠在運行 Android 2.1(API 級別 7)或更高版本的設備上提供 Material Design 體驗,但除非設備運行的是 Android 5.0(API 級別 21)或更高版本,否則原生操作欄不會支持 Material Design。
所以,官方建議使用支持庫的Toolbar來實現所有Activity的運用欄。
以下步驟說明了如何將 Toolbar 設置為 Activity 的應用欄:
在應用清單中,將 <application>
元素設置為使用 appcompat 的其中一個 NoActionBar 主題,或者針對某個Activity單獨設置主題。使用這些主題中的一個可以防止應用使用原生 ActionBar 類提供應用欄。例如:
或者為某個Activity設置主題:
<activity android:name="...MyActivity" ... android:theme="@style/A4.在 Activity 的布局添加一個 Toolbar:<android.support.v7.widget.Toolbar android:id="@+id/my_toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary"/>在 Activity 的 onCreate() 方法中,調用 Activity 的 setSupportActionBar() 方法,然后傳遞 Activity 的工具欄。該方法會將工具欄設置為 Activity 的應用欄。例如:
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar); setSupportActionBar(myToolbar);}應用欄空間很有限,如果一個應用程序定義了過多按鈕,應用欄將多余的按鈕放在溢出菜單。運用欄還可以指定一個按鈕應該始終顯示在溢出菜單還是顯示在應用欄中。
運用欄上所有的按鈕都是定義在 res/menu/ 目錄下的 menu resource文件中,所以,添加操作按鈕即是添加一個menu文件,并在Activity中onCreateOptionsMenu
指定menu文件。
定義運用欄按鈕:
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <!-- should appear as action button if possible --> <item android:id="@+id/action_favorite" android:icon="@drawable/ic_favorite_black_48dp" android:title="@string/action_favorite" app:showAsAction="ifRoom"/> <!--should always be in the overflow --> <item android:id="@+id/action_settings" android:title="@string/action_settings" app:showAsAction="never"/></menu>在Activity中onCreateOptionsMenu
指定menu文件:
當用戶選擇一個應用欄目時,系統調用Activity中的onoptionsitemselected()回調方法,并通過一個MenuItem對象表示這項被點擊。
@Overridepublic boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_settings: ... return true; case R.id.action_favorite: ... return true; default: // Invoke the superclass to handle it. return super.onOptionsItemSelected(item); }}新聞熱點
疑難解答