How to show Toolbar's logo, icon, title, subtitle when wrapped in a CollapsingToolbarLayout?

前端 未结 2 1049
梦毁少年i
梦毁少年i 2020-12-30 13:59

After Android Support Design Library was released, I wanted to implement an effect like a page of Twitter Profile, in which Toolbar

2条回答
  •  孤城傲影
    2020-12-30 14:44

    This issue was reported here as a bug. I have personally experienced this with one of my apps. Setting the topMargin for the toolbar works for me. As somebody commented on the bug report, here is the fix, as a function from the issue report.

    private void fixApi21ToolBarBug(Toolbar toolbar){
        if(Build.VERSION.SDK_INT!=21) return; //only on api 21
        final int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        final int result = (resourceId > 0) ? getResources().getDimensionPixelSize(resourceId) * 2 : 0;
        final CollapsingToolbarLayout.LayoutParams params = (CollapsingToolbarLayout.LayoutParams) toolbar.getLayoutParams();
        params.topMargin -= result;
        toolbar.setLayoutParams(params);
    }
    

    See the result here.

    enter image description here

提交回复
热议问题