How do I change an Android Snackbar's initial alignment from bottom to top?

六眼飞鱼酱① 提交于 2019-11-29 17:11:30

问题


The recent android library came out just a few days ago, but I would like to have the SnackBar appear on top of the screen, preferably within a RelativeLayout as it's parent view.

How does one change the SnackBar's initial alignment which I presume to be layout_alignParentBottom to layout_alignParentTop?


回答1:


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()

Note: The animation for the snackbar begins from the bottom and surges up to the top of the screen as expected because it was intended to be in the bottom as per ianhanniballake's answer.

For notifications surging from the top, it would probably better off getting a Custom banner instead.




回答2:


No, per the material design spec for Snackbars:

Snackbars provide lightweight feedback about an operation by showing a brief message at the bottom of the screen.

As the Design library seeks to implement the material design specs, it is not possible to position the Snackbar in any position other than the bottom of the screen (or containing CoordinatorLayout).




回答3:


Here's a simple way to add SnackBar using CoordinatorLayout from top of the screen but animation is still an issue.

Simply add a CoordinatorLayout layout to your layout like:

<android.support.design.widget.CoordinatorLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:id="@+id/snackbarlocation">
</android.support.design.widget.CoordinatorLayout>

and then in your Activity get a reference to it and pass it to the SnackBar as below:

    CoordinatorLayout Clayout = (CoordinatorLayout)findViewById(R.id.snackbarlocation);

    FabButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Snackbar.make(Clayout, "This snack bar located at top", Snackbar.LENGTH_SHORT).show();
        }
    });

Credits




回答4:


It can be done in simple ways

first Create the layout where snackbar to be included

<LinearLayout
    android:id="@+id/info_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"/>

then in Code

  ll_mesg.addView(Snackbar.make(ll_mesg, "Please choose your luck from one of box", Snackbar.LENGTH_LONG)
            .getView());
    new Handler().postDelayed(new Runnable() {
        public void run() {
            ll_mesg.removeAllViews();
            }
        }
        ,1000);



回答5:


Well, you can get snackbar's layoutparam and change the bottomMargin in the code. like

Snackbar snackbar = Snackbar.make(rootView, message, Snackbar.LENGTH_LONG);
View view = snackbar.getView();
FrameLayout.LayoutParams para = (CoordinatorLayout) view.getLayoutParams();
params.bottomMargin = showAtLocation // pass your location offset here
view.setLayoutParams(params);

Then you can use snackbar instance to show whenever you want

snackbar.show();


来源:https://stackoverflow.com/questions/30585550/how-do-i-change-an-android-snackbars-initial-alignment-from-bottom-to-top

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