編輯:關於 Android 程式設計
新版 QQ 換上了沉浸式狀態列,視覺上確實俐落不少。這篇就來示範怎麼在自己的 App 裡做出來。

先講清楚前提:半透明狀態列需要 4.4 以上才支援,而且 4.4 與 5.0 的呈現效果有差異。所以本文要解決兩件事:
先看模擬器與真機的對照,後面實作時可以回來比對。
4.4 模擬器:

5.x 真機:

本例用到了 NavigationView,所以版面會稍微長一些,不需要的部分可自行刪減。先補上依賴:
compile 'com.android.support:appcompat-v7:22.2.1' compile 'com.android.support:support-v4:22.2.1' compile 'com.android.support:design:22.2.0'
先定義幾個顏色,放在 res/values/color.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="primary">#FF03A9F4</color> <color name="primary_dark">#FF0288D1</color> <color name="status_bar_color">@color/primary_dark</color> </resources>
接著是 values/styles.xml:
<resources> <style name="BaseAppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/primary</item> <item name="colorPrimaryDark">@color/primary_dark</item> <item name="colorAccent">#FF4081</item> </style> <!-- Base application theme. --> <style name="AppTheme" parent="@style/BaseAppTheme"> </style> </resources>
再來是 values-v19/styles.xml,注意資料夾路徑:
<resources> <style name="AppTheme" parent="@style/BaseAppTheme"> <item name="android:windowTranslucentStatus">true</item> </style> </resources>
這段沒什麼玄機。要留意的是主題基於 NoActionBar,而 android:windowTranslucentStatus 這個屬性從 v19 才開始提供。
activity_main.xml:
<android.support.v4.widget.DrawerLayout 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" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/id_main_content" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.Toolbar android:id="@+id/id_toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" android:fitsSystemWindows="true" app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> <TextView android:id="@+id/id_tv_content" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:text="HelloWorld" android:textSize="30sp"/> </LinearLayout> <android.support.design.widget.NavigationView android:id="@+id/id_nv_menu" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" app:headerLayout="@layout/header_just_username" app:menu="@menu/menu_drawer" /> </android.support.v4.widget.DrawerLayout>
結構很直觀:DrawerLayout 裡一個 LinearLayout 當內容區,一個 NavigationView 當抽屜選單。注意 Toolbar 高度設為 wrap_content。
NavigationView 還依賴一個 header 版面,header_just_username.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="192dp" android:background="?attr/colorPrimaryDark" android:orientation="vertical" android:padding="16dp" android:fitsSystemWindows="true" android:theme="@style/ThemeOverlay.AppCompat.Dark"> <TextView android:id="@+id/id_link" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="16dp" android:text="https://blog.csdn.net/lmj623565791"/> <TextView android:id="@+id/id_username" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@id/id_link" android:text="Zhang Hongyang"/> <ImageView android:layout_width="72dp" android:layout_height="72dp" android:layout_above="@id/id_username" android:layout_marginBottom="16dp" android:src="@mipmap/ic_launcher"/> </RelativeLayout>
menu 檔就不再貼了,有興趣可參考官方 Design Support Library 的 NavigationView 說明。
看完版面,有四個地方要特別注意:
少了這個屬性,狀態列和 Toolbar 就會擠成一團:

package com.zhy.colorfulstatusbar;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.id_toolbar);
setSupportActionBar(toolbar);
//StatusBarCompat.compat(this, getResources().getColor(R.color.status_bar_color));
//StatusBarCompat.compat(this);
}
}
就這樣,一句 setSupportActionBar 帶過。此時 4.4 的效果如下:

還不錯,有個自然的漸層。而 5.x 的呈現是這樣:

5.x 預設並不是漸層,而是一塊偏深的純色。回頭看 Material Design 規範:

狀態列應該是比 Toolbar 背景再深一點的顏色。看來 4.4 這邊確實需要補一點適配工作。
思路其實很簡單:4.4 加上 windowTranslucentStatus 之後,我們就能使用狀態列那塊區域了。既然能用,那就在根版面塞一個與狀態列等高的 View,把背景設成我們想要的顏色即可。
package com.zhy.colorfulstatusbar;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by zhy on 15/9/21.
*/
public class StatusBarCompat {
private static final int INVALID_VAL = -1;
private static final int COLOR_DEFAULT = Color.parseColor("#20000000");
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void compat(Activity activity, int statusColor) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (statusColor != INVALID_VAL) {
activity.getWindow().setStatusBarColor(statusColor);
}
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
&& Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
int color = COLOR_DEFAULT;
ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
if (statusColor != INVALID_VAL) {
color = statusColor;
}
View statusBarView = new View(activity);
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity));
statusBarView.setBackgroundColor(color);
contentView.addView(statusBarView, lp);
}
}
public static void compat(Activity activity) {
compat(activity, INVALID_VAL);
}
public static int getStatusBarHeight(Context context) {
int result = 0;
int resourceId = context.getResources()
.getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = context.getResources().getDimensionPixelSize(resourceId);
}
return result;
}
}
邏輯寫得很直白:找到 android.R.id.content,在其中插入一個高度等於狀態列、背景色可自訂(預設半透明黑)的 View。
使用時只要在 Activity 裡寫:
StatusBarCompat.compat(this);
想自訂顏色的話:
StatusBarCompat.compat(this, getResources().getColor(R.color.status_bar_color));
一行搞定 4.4 到 5.x 的差異,算是相當划算。
最後補充一個坑:5.0 提供了 setStatusBarColor 可以直接設定狀態列顏色,但使用這個方法時,主題裡就不能再宣告 windowTranslucentStatus。解法是另開一個 values-v21 資料夾,styles.xml 寫成:
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="@style/BaseAppTheme"> </style> </resources>
也就是把 windowTranslucentStatus 拿掉。接著我們試著把狀態列設為紅色:
4.4 模擬器:

5.x 真機:

兩邊視覺一致,收工。
Android 指紋辨識系統:原理、API 與低版本適配實戰
Google 在 2015 年的 Android 6.0 發表會上宣布系統層級支援指紋辨識。雖然比 Apple 晚了一些,但對廣大 Android 使用者而言是個福音,對
Android 開發實戰:打造一個簡單的鬧鐘提醒
本實例將帶你動手做一個簡單的鬧鐘:透過 TimePickerDialog 時間選擇對話方塊讓使用者設定鬧鐘時間,再利用 AlarmManager 全域定時器,在指定時刻啟
高仿微信支付密碼輸入框:動態密碼格 + 底部彈出數字鍵盤
支付密碼輸入框是 App 裡再常見不過的元件,但自己做過的人都知道細節不少。本文把它拆成三塊來做,每一塊都可以單獨複用。 設計思路 Ⅰ. 密碼格數量由程式碼動態生成——
手機QQ瀏覽器如何清理緩存釋放空間
手機QQ瀏覽器用久了,緩存會越積越多,表現出來就是啟動變慢、頁面加載遲鈍,同時還白白佔用存儲空間。定期清理緩存是個簡單有效的小習慣。以下以手機QQ瀏覽器 V5.6.0 為