findViewById(android.R.id.statusBarBackground) returns null

笑着哭i 提交于 2019-12-22 01:13:56

问题


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);

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

What's wrong?


回答1:


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;
        }
    });


来源:https://stackoverflow.com/questions/47158795/findviewbyidandroid-r-id-statusbarbackground-returns-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!