我需要创建自定义动画 toast 消息。现在我需要知道这是否可能。我已经创建了一个带有自定义 View 的 toast,但我不知道如何向 toast 添加自定义动画。
这是我到目前为止的代码。
private void showToast() {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.custom_toast_layout_id));
// set a message
TextView text = (TextView) layout.findViewById(R.id.toast_text);
text.setText("Button is clicked!");
// Toast...
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
});
}
最佳答案
使用普通的 Android Toast 类无法做到这一点。 Stock 样式的 toast(添加到 WindowManager 而不是 ViewGroup 的)仅限于四个系统动画,并且不会接受来自您的项目的动画。如果您想在普通类型 Android Toast 中使用不同的系统动画,请查看我的 SuperToasts library 中的操作方法。 。为一个实例编写这样一个类可能不值得,因此我建议您使用我的库(如果您发现它有用)或编写一个类似于 toast 的自定义 View 类。您可以在this中看到我是如何做到这一点的。库的类。
关于Android为Toast添加自定义动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18034362/