简体   繁体   English

小吃店的自定义幻灯片动画

[英]custom slide animation of snackbar

I made a snackbar on the top of my layout.我在布局的顶部做了一个小吃店。 BUT, when it appears, it slides upwards and when it is dismissed, it slides downwards.但是,当它出现时,它向上滑动,当它消失时,它向下滑动。 My wish is to make it appear from the top of the screen and slides down, and vice-versa.我的愿望是让它从屏幕顶部出现并向下滑动,反之亦然。

CoordinatorLayout coordinatorLayout = findViewById(R.id.v_set_bgs);
snackbar = Snackbar.make(coordinatorLayout, R.string.changes, Snackbar.LENGTH_INDEFINITE);
View view = snackbar.getView();
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) view.getLayoutParams();
params.gravity = Gravity.START;
TextView snackbarText = view.findViewById(android.support.design.R.id.snackbar_text);
snackbarText.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
view.setLayoutParams(params);
view.setAlpha(0.75f);
if (snackbarShow && isAlready == 0 && AppSettings.isChanged) {
    isAlready = 1;
    snackbar.show();
}

So.所以。 So far, it's working like a normal snackbar (on the top of the layout).到目前为止,它就像一个普通的小吃店(在布局的顶部)。 I just couldn't find any method how to change the slide direction.我只是找不到任何方法来改变滑动方向。

According to the documentation根据文档

Snackbars provide lightweight feedback about an operation. Snackbars 提供关于操作的轻量级反馈。 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. Snackbar 出现在屏幕上所有其他元素的上方,并且一次只能显示一个。

The best option you can do is extend Snackbar class and override the show method or another whatever is responsible for the animation您可以做的最佳选择是扩展Snackbar类并覆盖show方法或其他负责动画的方法

This makes the Snackbar appears on Top without the strange slide in transition.这使得 Snackbar 出现在顶部而没有奇怪的过渡幻灯片。

Best simple solution in Kotlin: Kotlin 中最好的简单解决方案:

val snackbar = Snackbar.make(view, string, Snackbar.LENGTH_LONG)
val layoutParams = LayoutParams(snackbar.view.layoutParams)

layoutParams.gravity = Gravity.TOP
snackbar.view.setPadding(0, 10, 0, 0)
snackbar.view.layoutParams = layoutParams
snackbar.animationMode = BaseTransientBottomBar.ANIMATION_MODE_FADE
snackbar.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM