Android实现状态栏(statusbar)渐变效果的示例

前言

qq最近更新搞了渐变式状态栏.然后…新需求就是要加这个.唉

先来张效果图:

常见的方式:

设置Theme,状态栏透明.

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

实现起来比较简单.

比如:SystemBarTint

几行代码搞定了.

但是.我要泼冷水,这种方式会引起这些问题:

1.软键盘弹起bug2.fragment不适应

这种方式的优点:

兼容到4.4

我的看法:

1.用全屏模式或者状态栏透明的方式去实现,感觉很勉强2.为了一个statusbar的效果,然后去处理一大堆不适应,我认为是不值得的.特别是软键盘弹出的问题3.如果为了适配4.4,要费这么大功夫的话…

我的实现方式:

1.拿到StatusBar:

很简单,翻翻DecorView源码,就能知道,是通过ColorViewState 配置创建的View.

由于是私有成员,要拿到对应的参数,一般做法是使用反射.

这里我取了点巧,既然是View,那么就能findviewbyId找到

只要想办法拿到com.android.internal.R.id.statusBarBackground这个id值就行了

 private void initStatusBar() {    if (statusBarView == null) {      //android系统级的资源id得这么拿,不然拿不到      int identifier = getResources().getIdentifier("statusBarBackground", "id", "android");      statusBarView = getWindow().findViewById(identifier);    }    if (statusBarView != null) {      statusBarView.setBackgroundResource("你的渐变drawable资源id");    }  }

2.等StatusBar绘制完成

如果你直接在onCreate中调用上面的方法,你会发现,拿到的是null.

这是因为Statusbar还没绘制完成.

所以,可以在onCreate()中使用Looper.myQueue().addIdleHandler()来保证Statusbar绘制完成后再findview.

然后对DecorView设置addOnLayoutChangeListener监听

当布局发生变化,就设置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:

其实不加监听,也能实现改变statusbar颜色的效果..但是会出现问题

比如弹软键盘后,弹popwindow后,引起window状态改变时,statusbar的颜色就会复原…

基本完整的代码

  private View statusBarView;  @Override  protected void onCreate(Bundle savedInstanceState) {    //延时加载数据.    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;  }

这种方式的缺点:

1.因为没有使用全屏的模式,所以适配4.4是没戏了.

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

可是旅行的彼时那刻我的心情一直是好的吗?

Android实现状态栏(statusbar)渐变效果的示例

相关文章:

你感兴趣的文章:

标签云: