Android實(shí)現(xiàn)狀態(tài)欄(statusbar)漸變效果的示例
前言
qq最近更新搞了漸變式狀態(tài)欄.然后...新需求就是要加這個(gè).唉
先來張效果圖:

常見的方式:
設(shè)置Theme,狀態(tài)欄透明.
<item name="android:windowTranslucentStatus">true</item>
實(shí)現(xiàn)起來比較簡單.
幾行代碼搞定了.
但是.我要潑冷水,這種方式會(huì)引起這些問題:
1.軟鍵盤彈起bug
2.fragment不適應(yīng)
這種方式的優(yōu)點(diǎn):
兼容到4.4
我的看法:
1.用全屏模式或者狀態(tài)欄透明的方式去實(shí)現(xiàn),感覺很勉強(qiáng)
2.為了一個(gè)statusbar的效果,然后去處理一大堆不適應(yīng),我認(rèn)為是不值得的.特別是軟鍵盤彈出的問題
3.如果為了適配4.4,要費(fèi)這么大功夫的話...
我的實(shí)現(xiàn)方式:
1.拿到StatusBar:
很簡單,翻翻DecorView源碼,就能知道,是通過ColorViewState 配置創(chuàng)建的View.


由于是私有成員,要拿到對應(yīng)的參數(shù),一般做法是使用反射.
這里我取了點(diǎn)巧,既然是View,那么就能findviewbyId找到
只要想辦法拿到com.android.internal.R.id.statusBarBackground這個(gè)id值就行了
private void initStatusBar() {
if (statusBarView == null) {
//android系統(tǒng)級的資源id得這么拿,不然拿不到
int identifier = getResources().getIdentifier("statusBarBackground", "id", "android");
statusBarView = getWindow().findViewById(identifier);
}
if (statusBarView != null) {
statusBarView.setBackgroundResource("你的漸變drawable資源id");
}
}
2.等StatusBar繪制完成
如果你直接在onCreate中調(diào)用上面的方法,你會(huì)發(fā)現(xiàn),拿到的是null.
這是因?yàn)镾tatusbar還沒繪制完成.
所以,可以在onCreate()中使用Looper.myQueue().addIdleHandler()來保證Statusbar繪制完成后再findview.
然后對DecorView設(shè)置addOnLayoutChangeListener監(jiān)聽
當(dāng)布局發(fā)生變化,就設(shè)置statusbar的背景
Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
@Override
public boolean queueIdle() {
if (isStatusBar()) {
initStatusBar();
getWindow().getDecorView().addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
initStatusBar();
}
});
}
return false;
}
});
3.為什么要addOnLayoutChangeListener:
其實(shí)不加監(jiān)聽,也能實(shí)現(xiàn)改變statusbar顏色的效果..但是會(huì)出現(xiàn)問題
比如彈軟鍵盤后,彈popwindow后,引起window狀態(tài)改變時(shí),statusbar的顏色就會(huì)復(fù)原...
基本完整的代碼
private View statusBarView;
@Override
protected void onCreate(Bundle savedInstanceState) {
//延時(shí)加載數(shù)據(jù).
Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
@Override
public boolean queueIdle() {
if (isStatusBar()) {
initStatusBar();
getWindow().getDecorView().addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
initStatusBar();
}
});
}
//只走一次
return false;
}
});
}
private void initStatusBar() {
if (statusBarView == null) {
int identifier = getResources().getIdentifier("statusBarBackground", "id", "android");
statusBarView = getWindow().findViewById(identifier);
}
if (statusBarView != null) {
statusBarView.setBackgroundResource("你的漸變drawable資源id");
}
}
protected boolean isStatusBar() {
return true;
}
這種方式的缺點(diǎn):
1.因?yàn)闆]有使用全屏的模式,所以適配4.4是沒戲了.
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android Usb設(shè)備的監(jiān)聽(Dev)外設(shè)端口的判定以及耳機(jī)的插拔
- C# WPF使用AForge類庫操作USB攝像頭拍照并保存
- android通過usb讀取U盤的方法
- Android Studio使用USB真機(jī)調(diào)試詳解
- Android StatusBar 透明化方法(不同的版本適配)
- Android串口通信封裝之OkUSB的示例代碼
- Android 利用廣播監(jiān)聽usb連接狀態(tài)(變化情況)
- 詳解Android USB轉(zhuǎn)串口通信開發(fā)基本流程
- C語言實(shí)現(xiàn)模擬USB對8bit數(shù)據(jù)的NRZI編碼輸出
相關(guān)文章
Android 自定義標(biāo)題欄的實(shí)例詳解
這篇文章主要介紹了 Android 自定義標(biāo)題欄的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家實(shí)現(xiàn)這樣類似的功能,需要的朋友可以參考下2017-10-10
Android后臺(tái)啟動(dòng)Activity的實(shí)現(xiàn)示例
這篇文章主要介紹了Android后臺(tái)啟動(dòng)Activity的實(shí)現(xiàn)示例,幫助大家更好的理解和學(xué)習(xí)使用Android開發(fā),感興趣的朋友可以了解下2021-04-04
Android xmlns 的作用及其自定義實(shí)例詳解
這篇文章主要介紹了 Android xmlns 的作用及其自定義實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06
簡單實(shí)現(xiàn)安卓里百度地圖持續(xù)定位
本文主要介紹了在安卓的百度地圖開發(fā)里面簡單實(shí)現(xiàn)持續(xù)定位的方法,具有一定的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01
Android仿iOS側(cè)滑退出當(dāng)前界面功能
這篇文章主要為大家詳細(xì)介紹了Android仿iOS側(cè)滑退出當(dāng)前界面功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android ActionBarActivity設(shè)置全屏無標(biāo)題實(shí)現(xiàn)方法總結(jié)
這篇文章主要介紹了Android ActionBarActivity設(shè)置全屏無標(biāo)題實(shí)現(xiàn)方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-04-04

