源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2021-10-15 16:33 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android实现状态栏(statusbar)渐变效果的示例
[b]前言[/b] qq最近更新搞了渐变式状态栏.然后...新需求就是要加这个.唉 [b]先来张效果图:[/b] [img]http://img.1sucai.cn/uploads/article/2018010710/20180107100157_0_68086.png[/img] 常见的方式: 设置Theme,状态栏透明.
 <item name="android:windowTranslucentStatus">true</item>
实现起来比较简单. 比如:[url=https://github.com/jgilfelt/SystemBarTint]SystemBarTint[/url] 几行代码搞定了. 但是.我要泼冷水,这种方式会引起这些问题: 1.软键盘弹起bug 2.fragment不适应 [b]这种方式的优点:[/b] 兼容到4.4 [b]我的看法:[/b] 1.用全屏模式或者状态栏透明的方式去实现,感觉很勉强 2.为了一个statusbar的效果,然后去处理一大堆不适应,我认为是不值得的.特别是软键盘弹出的问题 3.如果为了适配4.4,要费这么大功夫的话... [b]我的实现方式:[/b] 1.拿到StatusBar: 很简单,翻翻DecorView源码,就能知道,是通过ColorViewState 配置创建的View. [img]http://img.1sucai.cn/uploads/article/2018010710/20180107100157_1_61567.png[/img] [img]http://img.1sucai.cn/uploads/article/2018010710/20180107100158_2_92609.png[/img] 由于是私有成员,要拿到对应的参数,一般做法是使用反射. 这里我取了点巧,既然是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的颜色就会复原... [b]基本完整的代码[/b]
  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;
  }

[b]这种方式的缺点:[/b] 1.因为没有使用全屏的模式,所以适配4.4是没戏了. 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部