How to fix the Snackbar height and position?

后端 未结 7 1121
甜味超标
甜味超标 2021-02-08 03:16

On Android Support Library 24.1.1, the Snackbar was working fine:

Then starting on Android Support Library 24.2.0 onwards, the

7条回答
  •  误落风尘
    2021-02-08 03:50

    I recently solved this by subtracting the navigation bar height from the bottom margin of the Snackbar view.

    First we need the navigation bar height. I found code for that in the answer marked as correct here: How to REALLY get the navigation bar height in Android

    Next, use the following code to adjust the Snackbar bottom margin:

    final Snackbar snackbar = Snackbar.make(findViewById(R.id.fullscreen_content),
                    message, Snackbar.LENGTH_LONG);
    
    View snackbarView = snackbar.getView();
    
    // Adjust Snackbar height for fullscreen immersive mode
    int navbarHeight = getNavigationBarSize(this).y;
    
    CoordinatorLayout.LayoutParams parentParams = (CoordinatorLayout.LayoutParams) snackbarView.getLayoutParams();
        parentParams.setMargins(0, 0, 0, 0 - navbarHeight);
        snackbarView.setLayoutParams(parentParams);
    
    snackbar.show();
    

    Note that I used the LayoutParams of a CoordinatorLayout. You should replace CoordinatorLayout with whichever parent layout type you have passed in to your Snackbar.make() function (in my case, R.id.fullscreen_content is a CoordinatorLayout). The nice thing about using CoordinatorLayout is that it allows Snackbars to be dismissed by swiping as a standard behavior.

提交回复
热议问题