栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

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

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

前言

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

先来张效果图:


常见的方式:

设置Theme,状态栏透明.

 true

实现起来比较简单.

比如:SystemBarTint

几行代码搞定了.

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

1.软键盘弹起bug
2.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是没戏了.

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/157773.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号