有没有办法创建自定义 toast 通知?

发布于 2024-10-10 09:52:02 字数 52 浏览 0 评论 0原文

我想创建一条具有自定义背景和自定义圆形边框的 Toast 消息。但似乎我无法找到解决方案。

I want to create a Toast message with custom background and custom rounded border. But it seems I am not able to find the solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

征﹌骨岁月お 2024-10-17 09:52:03

此链接是一个起点 - 不确定可以自定义 toast 的哪些元素:

http://developer.android.com/guide/topics/ui/notifiers/toasts.html#CustomToastView

此链接(带有示例 XML)显示了如何更改 toast 背景和其他属性

而此链接显示了边距自定义的可能性(在代码中):

http: //developer.android.com/reference/android/widget/Toast.html#setMargin(float, float)

This link is a starting point - not sure about which elements of the toast can be customised:

http://developer.android.com/guide/topics/ui/notifiers/toasts.html#CustomToastView

This link (with the example XML) shows how you can change the toast background and other attributes

whereas this link shows the margin customisation possibilities (in code):

http://developer.android.com/reference/android/widget/Toast.html#setMargin(float, float)

风吹雪碎 2024-10-17 09:52:03

下面是自定义Toast的代码:

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup)findViewById(R.id.toast_layout_root));
TextView text = (TextView) layout.findViewById(R.id.txtToast);
text.setTypeface(typeface_obj);
setText(ArabicClass.Convert(getResources().getString(R.string.ar_netork_failure)));
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 200);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

Here is code to customize Toast:

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup)findViewById(R.id.toast_layout_root));
TextView text = (TextView) layout.findViewById(R.id.txtToast);
text.setTypeface(typeface_obj);
setText(ArabicClass.Convert(getResources().getString(R.string.ar_netork_failure)));
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 200);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
丶视觉 2024-10-17 09:52:03

Android中的自定义Toast可以轻松完成。
首先。使用您想要在 Toast 中使用的小部件创建您自己的 xml 布局,然后膨胀该布局。然后使用膨胀的布局查找视图对象并设置其内容,然后创建 Toast。因为代码会很长。我刚刚解释了我是如何做到的。

我在我的博客中对此进行了轻松解释: http://androiddesk.wordpress.com/2012/01/28/custom-notification-in-android-with-an-example/

希望这有帮助。

Custom Toast in android can be easily done.
First of all. Create your own xml layout with the widgets that you want in your Toast and then inflate the layout.Then use the inflated layout to find the view objects and the set its contents then create the Toast. As the code would be long. I've just explained how I've done it.

I've explained easily about this in my blog: http://androiddesk.wordpress.com/2012/01/28/custom-notification-in-android-with-an-example/

Hope this helped.

我最亲爱的 2024-10-17 09:52:03
Toast toast =  Toast.makeText(getApplicationContext(),"Welcome",Toast.LENGTH_LONG);

View view = toast.getView();

TextView v = (TextView) toast.getView().findViewById(android.R.id.message);

v.setTextColor(Color.WHITE);

toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL,0, 0);

ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#f43f10"));

view.setBackgroundDrawable(colorDrawable);

toast.show();
Toast toast =  Toast.makeText(getApplicationContext(),"Welcome",Toast.LENGTH_LONG);

View view = toast.getView();

TextView v = (TextView) toast.getView().findViewById(android.R.id.message);

v.setTextColor(Color.WHITE);

toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL,0, 0);

ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#f43f10"));

view.setBackgroundDrawable(colorDrawable);

toast.show();
朮生 2024-10-17 09:52:03

您可以在这里找到 http ://android-apps-blog.blogspot.com/2011/04/how-to-display-custom-toast-in-android.html 关于如何创建自定义 Toast 通知的好教程。

You can find here http://android-apps-blog.blogspot.com/2011/04/how-to-display-custom-toast-in-android.html a good tutorian on How to create a Custom Toast Notification.

ヤ经典坏疍 2024-10-17 09:52:03

如果您想要自定义 Toast,那么唯一最好的办法就是创建自定义对话框,或者您可以说自定义警报框,您可以使用对话框主题在应用程序中设计与 Toast 相同的设计。
要查看自定义框的教程,请参阅 YouTube 链接
http://www.youtube.com/watch?v=NBXMoMB9-k0

希望这对你有帮助..

If you want your custom toast than the only best thing is to create custom dialog or you can say custom alert box which you can design same as toast in your app by using themes of dialog box.
To see tutorial for custom box here is a youtube link
http://www.youtube.com/watch?v=NBXMoMB9-k0

Hope this will helpful to you..

孤者何惧 2024-10-17 09:52:02

我发现了两个我认为有用的例子......
昨天我只是在找它,因为我也对此感兴趣。

http://blog.webagesolutions.com/archives/161

helloandroid.com/tutorials/how-customize-toasts

无法发布多个链接 -.-
这就是为什么第二个是文本......

希望它有帮助:)

I found two I think useful examples...
Yesterday I just was looking for it, because I'm also interessted in it.

http://blog.webagesolutions.com/archives/161

helloandroid.com/tutorials/how-customize-toasts

Not able to post more than one link -.-
That's why the second one is as text...

Hope it helps :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文