APP Bar1,應(yīng)用欄或者操作欄,使用應(yīng)用欄可讓您的應(yīng)用與其他 Android 應(yīng)用保持一致,允許用戶快速了解如何使用您的應(yīng)用并獲得一流的體驗(yàn)。應(yīng)用欄的主要功能包括:
一個(gè)專用區(qū)域,可以標(biāo)識(shí)您的應(yīng)用并指示用戶在應(yīng)用中的位置;以可預(yù)測(cè)的方式訪問(wèn)搜索等重要操作;支持導(dǎo)航和視圖切換(通過(guò)標(biāo)簽頁(yè)或下拉列表);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 級(jí)別 11)開(kāi)始,所有使用默認(rèn)主題的 Activity 均使用 ActionBar 作為應(yīng)用欄。不過(guò),經(jīng)過(guò)不同 Android 版本的演化,應(yīng)用欄功能已逐漸添加到原生 ActionBar 中。因此,原生 ActionBar 的行為會(huì)隨設(shè)備使用的 Android 系統(tǒng)的版本而發(fā)生變化。相比之下,最新功能已添加到支持庫(kù)版本的 Toolbar 中,并且這些功能可以在任何能夠使用該支持庫(kù)的設(shè)備上使用。
因此,您應(yīng)使用支持庫(kù)的 Toolbar 類來(lái)實(shí)現(xiàn) Activity 的應(yīng)用欄。使用支持庫(kù)的工具欄有助于確保您的應(yīng)用在最大范圍的設(shè)備上保持一致的行為。例如,Toolbar 小部件能夠在運(yùn)行 Android 2.1(API 級(jí)別 7)或更高版本的設(shè)備上提供 Material Design 體驗(yàn),但除非設(shè)備運(yùn)行的是 Android 5.0(API 級(jí)別 21)或更高版本,否則原生操作欄不會(huì)支持 Material Design。
所以,官方建議使用支持庫(kù)的Toolbar來(lái)實(shí)現(xiàn)所有Activity的運(yùn)用欄。
以下步驟說(shuō)明了如何將 Toolbar 設(shè)置為 Activity 的應(yīng)用欄:
在應(yīng)用清單中,將 <application> 元素設(shè)置為使用 appcompat 的其中一個(gè) NoActionBar 主題,或者針對(duì)某個(gè)Activity單獨(dú)設(shè)置主題。使用這些主題中的一個(gè)可以防止應(yīng)用使用原生 ActionBar 類提供應(yīng)用欄。例如:
或者為某個(gè)Activity設(shè)置主題:
<activity android:name="...MyActivity" ... android:theme="@style/A4.在 Activity 的布局添加一個(gè) 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() 方法中,調(diào)用 Activity 的 setSupportActionBar() 方法,然后傳遞 Activity 的工具欄。該方法會(huì)將工具欄設(shè)置為 Activity 的應(yīng)用欄。例如:
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar); setSupportActionBar(myToolbar);}應(yīng)用欄空間很有限,如果一個(gè)應(yīng)用程序定義了過(guò)多按鈕,應(yīng)用欄將多余的按鈕放在溢出菜單。運(yùn)用欄還可以指定一個(gè)按鈕應(yīng)該始終顯示在溢出菜單還是顯示在應(yīng)用欄中。
運(yùn)用欄上所有的按鈕都是定義在 res/menu/ 目錄下的 menu resource文件中,所以,添加操作按鈕即是添加一個(gè)menu文件,并在Activity中onCreateOptionsMenu 指定menu文件。
定義運(yùn)用欄按鈕:
<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文件:
當(dāng)用戶選擇一個(gè)應(yīng)用欄目時(shí),系統(tǒng)調(diào)用Activity中的onoptionsitemselected()回調(diào)方法,并通過(guò)一個(gè)MenuItem對(duì)象表示這項(xiàng)被點(diǎn)擊。
@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); }}新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注