欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2017年09月20日 09:09:46   作者:Jlanglang  
本篇文章主要介紹了Android實現(xiàn)狀態(tài)欄(statusbar)漸變效果的示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下

前言

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

先來張效果圖:


常見的方式:

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

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

實現(xiàn)起來比較簡單.

比如:SystemBarTint

幾行代碼搞定了.

但是.我要潑冷水,這種方式會引起這些問題:

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

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

兼容到4.4

我的看法:

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

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

1.拿到StatusBar:

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


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

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

只要想辦法拿到com.android.internal.R.id.statusBarBackground這個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)用上面的方法,你會發(fā)現(xiàn),拿到的是null.

這是因為Statusbar還沒繪制完成.

所以,可以在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:

其實不加監(jiān)聽,也能實現(xiàn)改變statusbar顏色的效果..但是會出現(xiàn)問題

比如彈軟鍵盤后,彈popwindow后,引起window狀態(tài)改變時,statusbar的顏色就會復(fù)原...

基本完整的代碼

  private View statusBarView;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    //延時加載數(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.因為沒有使用全屏的模式,所以適配4.4是沒戲了.

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論