国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android實(shí)現(xiàn)狀態(tài)欄(statusbar)漸變效果的示例

2019-12-12 02:01:18
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

前言

qq最近更新搞了漸變式狀態(tài)欄.然后...新需求就是要加這個(gè).唉

先來(lái)張效果圖:


常見(jiàn)的方式:

設(shè)置Theme,狀態(tài)欄透明.

 <item name="android:windowTranslucentStatus">true</item>

實(shí)現(xiàn)起來(lái)比較簡(jiǎn)單.

比如:SystemBarTint

幾行代碼搞定了.

但是.我要潑冷水,這種方式會(huì)引起這些問(wèn)題:

1.軟鍵盤彈起bug
2.fragment不適應(yīng)

這種方式的優(yōu)點(diǎn):

兼容到4.4

我的看法:

1.用全屏模式或者狀態(tài)欄透明的方式去實(shí)現(xiàn),感覺(jué)很勉強(qiáng)
2.為了一個(gè)statusbar的效果,然后去處理一大堆不適應(yīng),我認(rèn)為是不值得的.特別是軟鍵盤彈出的問(wèn)題
3.如果為了適配4.4,要費(fèi)這么大功夫的話...

我的實(shí)現(xiàn)方式:

1.拿到StatusBar:

很簡(jiǎn)單,翻翻DecorView源碼,就能知道,是通過(guò)ColorViewState 配置創(chuàng)建的View.


由于是私有成員,要拿到對(duì)應(yīng)的參數(shù),一般做法是使用反射.

這里我取了點(diǎn)巧,既然是View,那么就能findviewbyId找到

只要想辦法拿到com.android.internal.R.id.statusBarBackground這個(gè)id值就行了

 private void initStatusBar() {    if (statusBarView == null) {      //android系統(tǒng)級(jí)的資源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還沒(méi)繪制完成.

所以,可以在onCreate()中使用Looper.myQueue().addIdleHandler()來(lái)保證Statusbar繪制完成后再findview.

然后對(duì)DecorView設(shè)置addOnLayoutChangeListener監(jiān)聽(tīng)

當(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)聽(tīng),也能實(shí)現(xiàn)改變statusbar顏色的效果..但是會(huì)出現(xiàn)問(wè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)闆](méi)有使用全屏的模式,所以適配4.4是沒(méi)戲了.

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 洪湖市| 望城县| 孝义市| 和田县| 茌平县| 济宁市| 萨嘎县| 镇赉县| 丰镇市| 绥化市| 龙泉市| 洞口县| 蓬溪县| 香格里拉县| 通道| 怀柔区| 娄烦县| 视频| 昌图县| 扬中市| 襄垣县| 古丈县| 牙克石市| 昌吉市| 新安县| 文成县| 扎囊县| 昌宁县| 渑池县| 瑞丽市| 法库县| 株洲市| 靖西县| 中西区| 上蔡县| 芒康县| 株洲市| 庄河市| 沙洋县| 临汾市| 邵阳市|