【问题标题】:Android add custom animation to ToastAndroid 为 Toast 添加自定义动画
【发布时间】:2013-08-03 15:27:48
【问题描述】:

我需要创建一个自定义动画 toast 消息。现在我需要知道这是否可能。我创建了一个带有自定义视图的 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 android-animation toast android-custom-view


【解决方案1】:

使用现有的 Android Toast 类是不可能做到这一点的。 Stock 风格的 toast(添加到 WindowManager 而不是 ViewGroup 的)仅限于四个系统动画,并且不会接受来自您的项目的动画。如果您想在库存类型 Android Toasts 中使用不同的系统动画,请查看我的 SuperToasts library 中的操作方法。为一个实例编写这样的类可能不值得,因此如果您发现它有用,我建议您使用我的库或编写类似于 toast 的自定义视图类。你可以在库的this 类中看到我是如何做到这一点的。

【讨论】:

    【解决方案2】:

    Toast 使用无法更改的系统显示,因此答案是否定的,您无法更改 toast 动画。 但是,您可以制作类似于 toast 的自己的视图,并根据需要对其进行动画处理。

    【讨论】:

      猜你喜欢
      • 2013-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多