Translucent/Transparent status bar + CoordinatorLayout + Toolbar + Fragment

后端 未结 8 1147
难免孤独
难免孤独 2020-12-05 07:23

I have following setup:

  • I\'m using AppCompat
  • MainActivity, that holds a fragment and has a toolbar, that\'s hiding when scrolling down
8条回答
  •  渐次进展
    2020-12-05 08:03

    Edit your styles.xml (v21) , add the following style

     
    

    You can change parent theme as per your liking, but now declare this theme in your AndroidManifest.xml file for a particular activity like this :

     
    

    This will let your content visible under the transparent actionbar.

    Now use the following to align your toolbar below the StatusBar properly, call this in your oncreate:

     toolbar.setPadding(0, getStatusBarHeight(), 0, 0);
    

    Get statusbar height using following :

    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
    

    Remove the following from your coordinator layout tags :

    android:fitsSystemWindows="true"
    

    Now in order to collapse your toolbar or hide it you may refer to this tutorial

    Make sure you are using following version of design support library, as it is bug free :

    compile 'com.android.support:design:23.1.0'

提交回复
热议问题