天天看點

android 複制狀态欄,Android狀态欄

Android狀态欄(Status Bar)顔色

Android在4.4版本推出一個透明狀态欄的概念,使手機頂部的狀态欄的顔色全透明。

在5.0版本推出了Material Design,可以修改狀态欄顔色。

是以4.4之前的版本是無法設定狀态欄顔色的。

在style檔案中設定狀态欄的顔色

@color/color_theme

@color/color_fed952

通過這張圖檔我們可以清楚地了解到各顔色命名值代表的意義

android 複制狀态欄,Android狀态欄

res/values/styles 檔案中定義基礎主題樣式:

res/values-v19/styles 檔案中定義相容主題樣式:

不添加代碼預設顯示效果為

效果如下

android 複制狀态欄,Android狀态欄

信号欄的顔色與布局一緻

android 複制狀态欄,Android狀态欄
android 複制狀态欄,Android狀态欄

當我的style是這樣時

@color/colorPrimary

@color/colorPrimaryDark

@color/colorAccent

我們在代碼中添加如下的代碼

//設定狀态欄的顔色透明

Window window = getWindow();

window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

效果如下

android 複制狀态欄,Android狀态欄

小結

xml中設定狀态欄透明通過

true

在代碼中表示為 (注意是4.4.+以上才能實作,最好添加上版本判斷)

//設定狀态欄的顔色透明

Window window = getWindow();

window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

預留

當我們在style中添加下面的内容,系統會為我們預留狀态欄的位置,狀态欄的顔色和我們的布局顔色一緻的

true

添加在總體style中

@color/colorPrimary

@color/colorPrimaryDark

@color/colorAccent

true

android 複制狀态欄,Android狀态欄

改變狀态欄顔色

Android 5.0.+,API 21起,我們可以改變狀态欄的顔色

1.清除透明欄

2.添加Flag來設定狀态欄

3.設定狀态欄顔色

//在Android 5.0.1以上使用,改變狀态欄顔色

Window window = getWindow();

//取消設定透明狀态欄,使 ContentView 内容不再覆寫狀态欄

window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

//需要設定這個 flag 才能調用 setStatusBarColor 來設定狀态欄顔色

window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

//設定狀态欄顔色

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

window.setStatusBarColor(Color.YELLOW);

}

在我的Android 7.0上的顯示效果(顔色沖擊感很強,哈哈)

android 複制狀态欄,Android狀态欄

Android 6.0字型自動變色

系統預設狀态欄的字型為白色,6.0的API新增了一個屬性來解決這一問題。即,如果我們設定的狀态欄顔色是接近于白色的話,可以在主題中添加以下屬性:

true

android 複制狀态欄,Android狀态欄

在V23中添加下面的内容

true

true

android 複制狀态欄,Android狀态欄

在我的魅族中不需要設定,字型就會自動變色,據說小米也行

代碼中設定方式為

Window window = getWindow();

View decor = window.getDecorView();

int ui = decor.getSystemUiVisibility();

if (lightBar) {//狀态欄為白色

ui |=View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;

} else {

ui &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;

}

decor.setSystemUiVisibility(ui);

或者直接設定不管什麼顔色都設定為黑色字型,隻針對 6.0+

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//23

getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

}

設定狀态欄效果

android 複制狀态欄,Android狀态欄
android 複制狀态欄,Android狀态欄
android 複制狀态欄,Android狀态欄

上圖分别為簡書,UC,網易新聞的狀态欄效果

當我們設定為

true

我們的布局會頂到狀态欄

android 複制狀态欄,Android狀态欄

當我們添加了fitSystemWindows留出狀态欄的位置,可是狀态欄的顔色顯示的是背景顔色,并不是我們想要的

true

android 複制狀态欄,Android狀态欄

我們可以采取下面的方式

1.設定統一的狀态欄顔色

2.在狀态欄透明的情況下,添加與狀态欄高度相同的布局(比較靈活,顔色可多變)

設定統一的狀态欄顔色

這種方式最簡單,我們隻需要兩步

1.設定為NoActionBar的主題

2.設定狀态欄顔色

android 複制狀态欄,Android狀态欄

(ps:在我的4.4.4的手機上,沒有效果)

添加狀态欄高度的布局(推薦)

當我們設定了信号欄透明,即沉浸式時

第一步:擷取狀态欄的高度,建立一個布局他的高度就是狀态欄的高度

第二步:在我們每次建立布局的時候都添加上這個布局并設定需要的顔色

import android.content.Context;

import android.os.Build;

import android.util.AttributeSet;

import android.util.Log;

import android.widget.LinearLayout;

public class CompatToolbar extends LinearLayout {

public CompatToolbar(Context context) {

this(context, null);

}

public CompatToolbar(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public CompatToolbar(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

setup();

}

public void setup() {

int compatPadingTop = 0;

// android 4.4

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

compatPadingTop = getStatusBarHeight();

}

this.setPadding(getPaddingLeft(), getPaddingTop() + compatPadingTop, getPaddingRight(), getPaddingBottom());

}

public int getStatusBarHeight() {

int statusBarHeight = 0;

//擷取status_bar_height資源的ID

int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");

if (resourceId > 0) {

statusBarHeight = getResources().getDimensionPixelSize(resourceId);

}

Log.d("CompatToolbar", "狀态欄高度:" + px2dp(statusBarHeight) + "dp");

return statusBarHeight;

}

//将px轉為dp

public float px2dp(float pxVal) {

final float scale = getContext().getResources().getDisplayMetrics().density;

return (pxVal / scale);

}

}

注意我這裡使用的LinearLayout布局,讓該布局位于内容的上方

使用方式:

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="com.example.com.refreshlayout.MainActivity">

android:layout_width="match_parent"

android:layout_height="wrap_content"/>

android:layout_width="match_parent"

android:layout_height="wrap_content" />

android:layout_height="wrap_content"

android:text="Hello World!"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintLeft_toLeftOf="parent"

app:layout_constraintRight_toRightOf="parent"

app:layout_constraintTop_toTopOf="parent" />

android 複制狀态欄,Android狀态欄

這種方式用起來挺不友善的,有沒有更好的方式呢?

如果狀态欄是白色的,系統預設字型顔色時白色的,要如何更改字型顔色呢?本篇有點長,從下一篇介紹吧。