findViewById(android.R.id.statusBarBackground)返回null [英] findViewById(android.R.id.statusBarBackground) returns null

查看:723
本文介绍了findViewById(android.R.id.statusBarBackground)返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取 StatusBarView (我需要它来进行活动共享元素转换动画)。

I'm trying to get StatusBarView (I need it for activity shared element transition animation).

我在活动中使用以下代码:

I use the following code in my activity:

View decorView = getWindow().getDecorView();
View statusBar = decorView.findViewById(android.R.id.statusBarBackground);

但是 statusBar 始终为 null
我在 onCreate()之后和 onResume()之后尝试了这段代码-无效。

But statusBar is always null. I tried this code after onCreate() and after onResume() - no effect.

出什么问题了?

推荐答案

那些只需要干净答案而又没有干净答案的人阅读由T.Vert链接的整个主题

Snippet for those who just need a clean answer without reading the whole topic linked by T.Vert

    final View decor = getWindow().getDecorView();
    decor.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            decor.getViewTreeObserver().removeOnPreDrawListener(this);
            View statusBar = decor.findViewById(android.R.id.statusBarBackground);
            return true;
        }
    });

这篇关于findViewById(android.R.id.statusBarBackground)返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆