SnackBar from top. Is this possible?

北慕城南 提交于 2019-12-30 04:06:26

问题


I wanted to provide the SnackBar animation from top, instead of the regular behavior that displays the SnackBar from bottom. Is this easily hackable?


回答1:


No it is not possible. The documentation states that

They show a brief message at the bottom of the screen on mobile and lower left on larger devices. Snackbars appear above all other elements on screen and only one can be displayed at a time.

You could use a third part library, like Crouton for instance




回答2:


it is possible. check this library made by me https://github.com/AndreiD/TSnackBar

basically you add 2 new animations for sliding from top, and change the gravity of the layout. That's all :)

Later Edit: there's a bug going on.... . if anyone wants to spend some time into fixing it we'd all appreciate it :)




回答3:


 CoordinatorLayout   coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
  Snackbar snackbar = Snackbar.make(coordinatorLayout, "Text", Snackbar.LENGTH_LONG);
   View view = snackbar.getView();
   CoordinatorLayout.LayoutParams params =(CoordinatorLayout.LayoutParams)view.getLayoutParams();
                params.gravity = Gravity.TOP;
                view.setLayoutParams(params);
      snackbar.show();



回答4:


EDIT: This solution renders Snackbar on top, but animation is from bottom.

It is possible, at least with Android Material library and a little trick. You can bind snackbar to a view that renders on top position like this: On activity_main.xml:

<FrameLayout>

    <!-- rest of the components here -->
    
    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/top_coordinator"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"/>
</FrameLayout>

On MainActivity.kt:

val snackbar = Snackbar.make(
    findViewById(R.id.top_coordinator),
    "Hello World",
    Snackbar.LENGTH_INDEFINITE
)

snackbar.show()


来源:https://stackoverflow.com/questions/31660067/snackbar-from-top-is-this-possible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!