AndroidTraining學(xué)習(xí)------Adding the Action Bar

Adding the Action Bar

Action bar的關(guān)鍵功能包括以下這些:

  • 在app中留一處專門的空間標識你的app并指明使用者所處的位
  • 通過可預(yù)見的方式進行重要的動作
  • 支持導(dǎo)航和視圖切換(標簽和下拉菜單)

Setting Up the Action Bar

創(chuàng)建一個最基本的action bar需要你的app使用一個支持action bar的activity主題。需要怎樣的主題取決于你的app支持的最低安卓版本。

Support Android 3.0 and Above Only

從Android 3.0開始,action bar包含在所有使用Theme.Holo主題(或者它的子主題)的activity中,當targetSdkVersion或minSdkVersion屬性設(shè)置為“11”或更高時,它是默認主題。

Support Android 2.1 and Above

當在比Android 3.0更老的版本上添加action bar時,需要你在你的應(yīng)用中導(dǎo)入Android支持庫。
一旦有了與你項目集成的支持庫:
1.更新你的activity,使它繼承ActionBarActivity,例如:

public class MainActivity extends ActionBarActivity{...}

2.在你的配置文件中,更新你的<application>或獨立的<activity>元素,使用Theme.AppCompat主題中的一個,例如:

<activity android:theme:"@style/Theme.AppCompat.Light"...>

Adding Action Bar

Action bar允許你添加與app當前上下文有聯(lián)系的最重要行動項目的按鈕。那些直接出現(xiàn)在action bar中的圖標和文字被稱為動作按鈕,不能在action bar或不那么重要的動作被隱藏在下拉菜單中。

Specify the Action Bar

所有的動作按鈕和下拉菜單中的項目都定義在一個XML menu resource中,為了添加action到action bar中,先在你的項目的res/menu/目錄中創(chuàng)建一個新的XML文件
為每一個你想放置在action bar中的項目添加<item>元素,例如:

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <!-- Search, should appear as action button -->
        <item
            android:id="@+id/action_search"
            android:icon="@drawable/ic_action_search"
            android:showAsAction="ifRoom"
            android:title="@string/action_search" />
        <!-- Settings, should always be in the overflow -->
        <item
            android:id="@+id/action_settings"
            android:showAsAction="never"
            android:title="@string/action_settings" />
    </menu>
  • showAsAction屬性有四個值:
  • always:這個值會使菜單項一直顯示在Action Bar上
  • ifRoom:如果有足夠空間,這個值會使菜單項顯示在Action Bar上
  • never:這個值使菜單項永遠不會顯示在ActionBar上
  • withText:這個值使菜單項和它的圖標,文本一起顯示
    注意:當為你的app創(chuàng)建icon或者bitmap圖片時,要為不同屏幕密度下的顯示效果提供多個優(yōu)化的版本,這一點非常重要。
    如果你要兼容Android2.1的support庫,在android:命名空間下showAsAction屬性不能使用。Support庫提供了替代的屬性,你必須定義你自己的XML命名空間并用該命名空間作為屬性的前綴。(一個自定義的XML命名空間應(yīng)該以你的app名稱為基礎(chǔ),但是可以取任何你想要的名字,它的作用域僅僅在你的聲明文件范圍之內(nèi))比如:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:yourapp="http://schemas.android.com/apk/res-auto"
         <!--Search,should appear as action button --->
         <item android:id="@+id/action_search"
               android:icon="@drawable/ic_action_search"
               android:title="@string/action_search"
               yourapp:showAsAction="ifRoom"/>
         ...
</menu>

Add the Action to the Action Bar

要為action bar布局菜單項,就要在activity中實現(xiàn)onCreateOptionsMenu()回調(diào)方法來inflate菜單資源從而獲取Menu對象,例如:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_activity_actions, menu);
        return super.onCreateOptionsMenu(menu);
    } 

Respond to Action Buttons

當用戶點擊某一個操作按鈕或者下拉菜單時,系統(tǒng)會調(diào)用activity中的onOptionsItemSelected()回調(diào)方法。在該方法的實現(xiàn)中會調(diào)用MenuItem的getItemId()方法去判斷哪個條目被點擊了--返回的ID會匹配我們聲明對應(yīng)的<item>元素中的android:id屬性的值。

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //Handle presses on the action bar items
        switch (item.getItemId()) {
            case R.id.action_search:
                Toast.makeText(MainActivity.this, "you click the search button", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.action_settings:
                Toast.makeText(MainActivity.this, "you click the settings button", Toast.LENGTH_SHORT).show();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

Add Up Button for Low-level Activities

在不是程序入口的所有其他屏中(activities不是“home”頁)需要為用戶提供一個導(dǎo)航到邏輯父屏的Up Button(向上按鈕)。
當運行在Android4.1或更高的版本,或者使用Support庫中的ActionBarActivity時,實現(xiàn)向上只需要你在manifest文件中定義父activity,并且設(shè)置action bar中的Up Button可用。比如:

    <activity android:name=".DisplayMessageActivity"
        android:label="@string/title_activity_display_message"
        android:parentActivityName=".MainActivity">
    </activity>

然后通過setDisplayHomeAsUpEnabled()將app icon設(shè)置成可用的Up按鈕:

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_displaymessage);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        //If your minSdkVersion is 11 or higher, instead use:
        //getActionBar().setDisplayHomeAsUpEnabled(true);
    }

因為系統(tǒng)現(xiàn)在知道MainActivity時DisplayMessageActivity的父activity,當用戶點擊Up按鈕時,系統(tǒng)會導(dǎo)航到恰當?shù)母竌ctivity--你需要處理Up Button的點擊事件。

Styling the Action Bar

Android包括少部分內(nèi)置的activity主題,這些主題包括"drak"或"light"action bar樣式。你可以擴展這些主題,以便更好地為action bar自定義外觀。
注意:如果你在action bar中使用了Support庫的API,你必須使用(或重寫)Theme.AppCompat家族樣式(甚至Theme.Holo家族樣式,在API level 11及以上可用)。如此一來,聲明的每個樣式屬性都必須聲明兩次:一次使用平臺的樣式屬性(android:屬性),一次使用Support庫的樣式屬性(appcompat.R.attr 屬性 -- 這些屬性的上下文其實就是我們的app)。

Use an Android Theme

Android包括兩種基本的activity主題,它們決定了action bar的顏色:

  • Theme.Holo for a "drak" theme.

  • Theme.Holo.light for a "light" theme.
    這些主題可以被應(yīng)用到整個app,也可以通過在manifest文件中設(shè)置<application>元素或<activity>元素的android:theme屬性,對單一的activities進行設(shè)置。比如:

      <application android:theme="@android:style/Theme.Holo.Light".../>
    

通過聲明Theme.Holo.Light.DarkActionBar可以使action bar為dark,而其余部分為light。
當使用Support庫時,必須用Theme.AppCompat主題替代:

  • Theme.AppCompat for the "dark" theme.
  • Theme.AppCompat.Light for the "light" theme.
  • Theme.AppCompat.Light.DarkActionBar for the light theme with a dark action bar.
    確保你使用的action bar icon和action bar本身有差異。

Customize the Background

為改變action bar的背景,可以通過為activity自定義一個主題,并重寫actionBarStyle屬性。該屬性指向另一個樣式,在這個樣式里,我們可以通過指定一個drawable資源來重寫background屬性。
如果你的app使用了navigation tabs或split action bar,你也可以通過分別設(shè)置backgroundStacked和backgroundSplit屬性來為bars指定背景。
警告:聲明合適的父主題非常重要,你自定義的主題和樣式繼承它們的樣式。如果沒有父樣式,你的action bar將會失去許多默認的樣式屬性,除非我們顯式的對它們進行聲明。

For Android 3.0 and higher only

僅當支持Android 3.0和更高版本時,你可以這樣定義action bar的背景:

  <?xml version="1.0" encoding="utf-8"?>
  <resources>
      <!--the theme applied to the application or activity-->
      <style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
          <item name="android:actionBarStyle">@style/MyActionBar</item>
      </style>

      <!--ActionBar styles-->
      <style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
          <item name="android:background">#ff0000</item>
      </style>
  </resources>

然后將我們的主題應(yīng)用到整個app中或單獨的activity中:

  android:theme="@style/CustomActionBarTheme"...>

For Android 2.1 and higer

當你使用Support庫時,上面同樣的主題必須替代如下:

  <?xml version="1.0" encoding="utf-8"?>
  <resources>
      <!--the theme applied to the application or activity-->
      <style name="CustomActionBarTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
          <item name="android:actionBarStyle">@style/MyActionBar</item>

          <!--Support libray compatibility-->
          <item name="actionBarStyle">@style/MyActionBar</item>
      </style>

      <!--ActionBar styles-->
      <style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
          <item name="android:background">#ff0000</item>

          <!--Support library compatibility-->
          <item name="background">@color/red</item>
      </style>
  </resources>

然后將我們的主題應(yīng)用到整個app中或單獨的activity中:

  android:theme="@style/CustomActionBarTheme"...>

Customize the Text Color

為了修改action bar文本的顏色,你需要重寫每個元素的屬性:

  • Action bar title:創(chuàng)建一個自定義樣式,并指定textColor屬性;同時在我們自定義的actionBarStyle中指定titleTextStyle屬性。
    注意:被應(yīng)用到titleTextStyle的自定義樣式應(yīng)該使用TextAppearance.Holo.Widget.ActionBar.Title 作為父樣式。
  • Action bar tabs:在我們的activity主題中重寫actionBarTabTextStyle。
  • Action buttons:在我們的activity主題中重寫actionMenuTextColor。

For Android 3.0 and higher only

當僅支持Android3.0和更高版本時,你的Style XML文件用該這樣:

  <?xml version="1.0" encoding="utf-8"?>
  <resources>
      <!--the theme applied to the application or activity-->
      <style name="CustomActionBarTheme" parent="@style/Theme.AppCompat">
          <item name="android:actionBarStyle">@style/MyActionBar</item>
          <item name="android:actionBarTabStyle">@style/MyActionBarTabText</item>
          <item name="android:actionMenuTextColor">@color/actionbar_text</item>
      </style>

      <!--ActionBar styles-->
      <style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar">
          <item name="android:titleTextStyle">@style/MyActionBarTitleStyle</item>

      </style>

      <!--ActionBar tab text-->
      <style name="MyActionBarTabText" parent="@style/Widget.AppCompat.ActionBar.TabText">
          <item name="android:textColor">@color/actionbar_text</item>
      </style>

      <!--ActionBar title text-->
      <style name="MyActionBarTitleStyle" parent="TextAppearance.AppCompat.Widget.ActionBar.Title">
          <item name="android:textColor">@color/actionbar_text</item>
      </style>
  </resources>

For Android 2.1 and higher

當時使用Support庫時,我們的style XML文件應(yīng)該是這樣的:
<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="CustomActionBarTheme" parent="@style/Theme.AppCompat">
<item name="android:actionBarStyle">@style/MyActionBar</item>
<item name="android:actionBarTabStyle">@style/MyActionBarTabText</item>
<item name="android:actionMenuTextColor">@color/actionbar_text</item>

          <!--Support libray compatibility-->
          <item name="actionBarStyle">@style/MyActionBar</item>
          <item name="actionBarTabTextStyle">@style/MyActionBarTabText</item>
          <item name="actionMenuTextColor">@color/actionbar_text</item>
      </style>

      <!--ActionBar styles-->
      <style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar">
          <item name="android:titleTextStyle">@style/MyActionBarTitleStyle</item>

          <!--Support library compatibility-->
          <item name="titleTextStyle">@style/MyActionBarTitleStyle</item>
      </style>

      <!--ActionBar tab text-->
      <style name="MyActionBarTabText" parent="@style/Widget.AppCompat.ActionBar.TabText">
          <item name="android:textColor">@color/actionbar_text</item>
      </style>

      <!--ActionBar title text-->
      <style name="MyActionBarTitleStyle" parent="TextAppearance.AppCompat.Widget.ActionBar.Title">
          <item name="android:textColor">@color/actionbar_text</item>
      </style>
  </resources>

Overlaying the Action Bar

默認情況下,action bar顯示在activity窗口的頂部,會稍微減少其他布局的可用空間。如果在用戶交互的過程中,你想要隱藏或顯示action bar可以調(diào)用hide()和show()來實現(xiàn)。然而,這將導(dǎo)致activity會基于它的新尺寸重新計算和重新繪制布局。
為避免在顯示和隱藏action bar時重新調(diào)整布局大小,我們可以在action bar上啟用疊加模式(overlay mode),布局可以使用所有的可用空間,就好像action bar不存在一樣,并且系統(tǒng)會將action bar疊加在布局之上。這樣布局頂部就會有點被遮擋,但是現(xiàn)在action bar隱藏或顯示時,系統(tǒng)就不需要重新調(diào)整布局的大小而是無縫過渡。

Enable Overlay Mode

要為action bar 啟用overlay mode,我們需要創(chuàng)建一個自定義主題,該主題繼承與一個已存在的action bar 主題,并且把android:windowActionBarOverlay屬性設(shè)置為true。

For Android 3.0 and higher only

如果minSDKVersion設(shè)為11或更高版本,我們的自定義主題應(yīng)該使用Theme.Holo 主題(或者它的一個子主題)作為父主題。比如:

  <resources>
      <!--the theme applied to the application or activity-->
      <style name="CustomActionBarTheme" parent="@style/Theme.Holo">
          <item name="android:windowActionBarOverlay">true</item>
      </style>
  </resources>

For Android 2.1 and higher

如果你的app為了兼容運行在Android 3.0之下的設(shè)備而使用了Support庫,你的自定義主題應(yīng)該使用Theme.AppCompat主題(或它的一個子主題)作為父主題。比如:

  <resources>
      <!--the theme applied to the application or activity-->
      <style name="CustomActionBarTheme" parent="@style/Theme.AppCompat">
          <item name="android:windowActionBarOverlay">true</item>
       
          <!--Support library compatibility-->
          <item name="windowActionBarOverlay">true</item>
      </style>
  </resources>

注意,這個主題包括兩種不同的windowActionBarOverlay樣式定義:一個帶有android:前綴,一個沒有。帶有android:前綴的適用于包括該樣式的Android系統(tǒng)版本,不帶前綴的適用于從Support讀取樣式的舊版本。

Specify Layout Top-margin

當action bar啟用了overlay mode,可能會遮擋某些本應(yīng)該可見的布局。為了確保這些布局始終處于action bar的下面,可以通過使用actionBarSize來指定頂部margin或padding的高度來實現(xiàn)。比如:

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingTop="?android:attr/actionBarSize">
  </LinearLayout>

如果你在action bar中使用了Support庫,你需要刪除android:前綴。比如:

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingTop="?android:attr/actionBarSize">
  </LinearLayout>

在這種情況下,不帶前綴的?attr/actionBarSize適用于包括Android3.0和更高版本的所有版本。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容