編輯:關於 Android 程式設計
Toolbar 是 Material Design 取代 ActionBar 的方案,彈性遠高於舊做法:它只是一個普通的 ViewGroup,想放什麼、怎麼排都由你決定。以下從最簡單的引入開始,一路把屬性、選單、樣式與事件處理走完。
環境說明:
引入與使用
XML 布局中加入:
<android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"/>
主題改為隱藏 ActionBar:
Theme.AppCompat.Light.NoActionBar
Activity 代碼中加入:
setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar);
此時運行效果:

加上背景色:
android:background="@color/colorPrimary"

基本屬性
<android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@color/colorPrimary" app:navigationIcon="@mipmap/title_bar_back"//左側圖標 app:subtitle="子標題" app:subtitleTextColor="#fff" //標題顏色 app:title="標題" app:titleTextColor="#fff"/> //子標題顏色

加入選項選單
第一步,建立選單資源:
<menu xmlns:android="https://schemas.android.com/apk/res/android" xmlns:app="https://schemas.android.com/apk/res-auto" xmlns:tools="https://schemas.android.com/tools" tools:context=".MainActivity"> <item android:id="@+id/action_settings" android:icon="@mipmap/ic_launcher" android:orderInCategory="100" android:title="settings" app:showAsAction="never"/> <item android:id="@+id/action_share" android:icon="@mipmap/ic_action_share" android:orderInCategory="100" android:title="settings" app:showAsAction="ifRoom"/> <item android:id="@+id/action_search" android:icon="@mipmap/ic_action_search" android:orderInCategory="100" android:title="settings" app:showAsAction="ifRoom"/> </menu>
第二步,重寫 onCreateOptionsMenu 載入它:
@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; }

個性化設定
左側返回箭頭:先用系統內建的,就得把剛才自訂的 navigationIcon 拿掉:
app:navigationIcon="@mipmap/title_bar_back"
然後在程式碼中啟用:
getSupportActionBar().setHomeButtonEnabled(true); //設置返回鍵可用 getSupportActionBar().setDisplayHomeAsUpEnabled(true);

溢出圖標顏色,在 style 中加入:
<!-- 溢出菜單圖標顏色--> <item name="colorControlNormal">@android:color/white</item>

自訂右側溢出圖標:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <!-- 溢出菜單圖標顏色--> <item name="colorControlNormal">@android:color/white</item> <!-- 溢出菜單圖標自定義--> <item name="android:actionOverflowButtonStyle">@style/OverflowButtonStyle</item> </style> <style name="OverflowButtonStyle" parent="android:Widget.ActionButton.Overflow"> <item name="android:src">@mipmap/ic_action_add</item> </style>

更改彈出選單背景,在 style 中定義後於版面套用:
app:popupTheme="@style/ToolbarPopupTheme"

更改彈出選單文字顏色:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <!-- 溢出菜單圖標顏色--> <item name="colorControlNormal">@android:color/white</item> <item name="android:actionOverflowButtonStyle">@style/OverflowButtonStyle</item> <!-- 溢出菜單文字顏色--> <item name="textAppearanceLargePopupMenu">@style/Overflow_Menu_Text_style</item> </style> <!--溢出菜單文字顏色--> <style name="Overflow_Menu_Text_style" parent="@style/TextAppearance.AppCompat.Widget.PopupMenu.Large"> <item name="android:textColor">#fff</item> </style>

修改標題文字大小,加上配置並定義 style:
app:titleTextAppearance="@style/ToolbarTitleSize"
<!-- toolbar標題文字大小 --> <style name="ToolbarTitleSize" parent="@style/TextAppearance.Widget.AppCompat.Toolbar.Title"> <item name="android:textSize">28sp</item> </style>

子標題寫法相同,換成 subtitleTextAppearance 並自訂 style 即可:
app:subtitleTextAppearance="@style/ToolbarTitleSize"
修改彈出選單位置
讓選單顯示在 Toolbar 下方而非蓋住它,關鍵是 overlapAnchor 設為 false:
app:popupTheme="@style/OverflowMenuStyle"
<style name="OverflowMenuStyle"> <!-- 是否覆蓋錨點,默認為true,即蓋住Toolbar --> <item name="overlapAnchor">false</item> <item name="android:dropDownWidth">wrap_content</item> <item name="android:paddingRight">5dp</item> <!-- 彈出層背景顏色 --> <item name="android:colorBackground">#FFCC99</item> <!-- 彈出層垂直方向上的偏移,即在豎直方向上距離Toolbar的距離,值為負則會蓋住Toolbar --> <item name="android:dropDownVerticalOffset">5dp</item> <!-- 彈出層水平方向上的偏移,即距離屏幕左邊的距離,負值會導致右邊出現空隙 --> <item name="android:dropDownHorizontalOffset">0dp</item> <!-- 設置彈出菜單文字顏色 --> <item name="android:textColor">#0099CC</item> </style>

事件處理
返回按鈕:
toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(), "點擊了返回箭頭", Toast.LENGTH_LONG).show(); } });
選單項點擊:
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_settings: break; case R.id.action_search: break; case R.id.action_share: break; } return true; }
自定義 Toolbar 內容
Toolbar 本身就是容器,直接往下嵌版面即可:
<android.support.v7.widget.Toolbar android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> ...... </RelativeLayout> </android.support.v7.widget.Toolbar>
搭配 DrawerLayout 做側滑選單
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="https://schemas.android.com/apk/res/android" android:id="@+id/drawer_left" android:layout_width="match_parent" android:layout_height="match_parent"> <!--側滑菜單--> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="start" android:background="#CCCCFF" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="選項一" android:textSize="18sp"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="選項二" android:textSize="18sp"/> </LinearLayout> </android.support.v4.widget.DrawerLayout>
在主版面中於 Toolbar 下方引入:
<!--DrawerLayout--> <include layout="@layout/custom_drawerlayout"/>
程式碼中建立關聯:
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_left); ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.open, R.string.close); mDrawerToggle.syncState(); mDrawerLayout.setDrawerListener(mDrawerToggle);

較新版 Studio 在建立 Activity 時可直接選擇對應範本,會自動把 DrawerLayout 建好並與 Toolbar 關聯起來,省下不少手勢。
總結
Toolbar 的強處在於「它只是個 View」:高度、背景、標題字級、選單彈出位置樣樣可改,配上 DrawerLayout 就能搭出完整的 Material 導覽架構。以上屬性與樣式名稱建議直接當成速查表使用。
Android 網路圖片載入:HttpURLConnection 搭配 Handler 更新 UI
要把網路上的圖片顯示到 ImageView,關鍵在於執行緒分工:網路連線必須放在子執行緒,UI 更新則必須回到主執行緒。Android 裡最直白的搭配就是 HttpURL
Android 屏幕適配、版本適配與科大訊飛語音集成實戰
屏幕適配 原型圖和設計圖: 800*480 —> 向下兼容 1280*720 —> 向上兼容 圖片適配: 根據屏幕的分辨率,選擇 dr
高通 Quick Charge 2.0 快充原理完整解析:從誕生背景到握手協議
高通 Quick Charge 2.0(以下簡稱 QC2.0)曾經是業界熱議的焦點。在它之前,快充只能靠提高電流來達成,但 MicroUSB 介面在 5V/2A(10W)
打造通用的 Android ListView、GridView 適配器
1、簡述 在 Android 開發中,與 adapter 打交道幾乎無可避免。一般做法是繼承 BaseAdapter、重寫其中幾個方法,然後一個 ListView 對應一