Android为Toast添加自定义动画

标签 android android-animation toast android-custom-view

我需要创建自定义动画 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/

相关文章:

android - java.lang.NullPointerException 等错误

javascript - 如何在 NativeScript 中从 Java 获取定义的实例/变量?

android - android中的富文本框

android - 动画时在 RecyclerView 内折叠 CardView

android - Toast 上的事件已隐藏

android - 无黑屏的 Activity 之间的平滑过渡

android - 开始后无法在android中停止Animator

android - 翻译动画部分的屏幕变为空白

java - 在 onActivityResult 中显示 toast

android - 如何 Toast ListView arrayadapter<string> 上的某个位置?