Translucent status bar and toolbar

后端 未结 8 1986
醉梦人生
醉梦人生 2020-12-16 12:43

I\'d like to integrate something like this:

And I\'ve done it like this, but I can\'t seem to put the imageview below the toolbar. Without the toolbar, I ca

8条回答
  •  抹茶落季
    2020-12-16 13:29

    Dont treat status bar as something separate from your app. Image is coming below the toolbar because you have used LinearLayout. Had you used RelativeLayout, your image would be starting at the same height as toolbar.

    Now for making the statusbar transparent and for everything to start from under the statusbar use the following.

    
    

    Use the above style for your activity and everything starts from under the statusbar. Now for the toolbar, you can increase the height of the toolbar by adding the height of the statusbar as padding to toolbar. This can be done as follows:

    toolbarContainer.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    Rect frame = new Rect();
                    getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
                    toolbarContainer.setPadding(0, frame.top, 0, 0);
                }
            });
    

    You can set a color to statusbar and use the same color with AlphaTransparency on Toolbar.

    getWindow().setStatusBarColor(ContextCompat.getColor(this, android.R.color.transparent));
    

    Now you control everything including the statusbar and the toolbar.

提交回复
热议问题