How to show Snackbar at top of the screen

后端 未结 9 1688
梦谈多话
梦谈多话 2020-12-02 07:33

As the Android documentation says

Snackbars provide lightweight feedback about an operation. They show a brief message at the bottom

9条回答
  •  半阙折子戏
    2020-12-02 08:12

    It is possible to make the snackbar appear on top of the screen using this:

    Snackbar snack = Snackbar.make(parentLayout, str, Snackbar.LENGTH_LONG);
    View view = snack.getView();
    FrameLayout.LayoutParams params =(FrameLayout.LayoutParams)view.getLayoutParams();
    params.gravity = Gravity.TOP;
    view.setLayoutParams(params);
    snack.show();
    

    From the OP:

    I had to change the first line:

    Snackbar snack = Snackbar.make(findViewById(android.R.id.content), "Had a snack at Snackbar", Snackbar.LENGTH_LONG);
    

提交回复
热议问题