我想创建一个带有自定义背景和自定义圆角边框的 Toast 消息。但似乎我无法找到解决方案。
7 回答
我发现了两个我认为有用的例子......昨天我正在寻找它,因为我也对它感兴趣。
http://blog.webagesolutions.com/archives/161
helloandroid.com/tutorials/how-customize-toasts
无法发布多个链接-.-这就是为什么第二个链接是文本的原因...
希望能帮助到你 :)
此链接是一个起点 - 不确定吐司的哪些元素可以自定义:
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)
这是自定义 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();
android中的自定义吐司可以很容易地完成。首先。使用您想要在 Toast 中的小部件创建您自己的 xml 布局,然后扩展布局。然后使用扩展布局查找视图对象并设置其内容,然后创建 Toast。因为代码会很长。我刚刚解释了我是如何做到的。
我已经在我的博客中轻松解释了这一点:http ://androiddesk.wordpress.com/2012/01/28/custom-notification-in-android-with-an-example/
希望这有帮助。
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();
你可以在这里找到http://android-apps-blog.blogspot.com/2011/04/how-to-display-custom-toast-in-android.html一个关于如何创建自定义 Toast 通知的好教程。
如果您想要自定义吐司,那么最好的办法是创建自定义对话框,或者您可以说自定义警报框,您可以使用对话框的主题在您的应用中设计与吐司相同的内容。要在此处查看自定义框的教程是一个 youtube 链接 http://www.youtube.com/watch?v=NBXMoMB9-k0
希望这对你有帮助..