可以通过编程方式将图像添加到吐司弹出窗口中吗?
发布于 2013-04-20 08:20:55
只需使用以下代码:
Toast toast = new Toast(myContext);
ImageView view = new ImageView(myContext);
view.setImageResource(R.drawable.image_icon);
toast.setView(view);
toast.show();
发布于 2011-09-27 23:55:36
您可以通过编程方式创建任何视图(因为我假设您正在询问如何在不使用LayoutInflater的情况下执行此操作),并在您制作的Toast上调用setView。
//Create a view here
LinearLayout v = new LinearLayout(this);
//populate layout with your image and text or whatever you want to put in here
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(v);
toast.show();
发布于 2013-11-08 20:49:43
Knickedi的解决方案很好,但如果您只需要文本旁边的图标,您可以利用Toast具有相同ID的预定义TextView这一事实,并在TextView上设置图标:
Toast toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
TextView tv = (TextView) toast.getView().findViewById(android.R.id.message);
if (null!=tv) {
tv.setCompoundDrawablesWithIntrinsicBounds(icon, 0, 0, 0);
tv.setCompoundDrawablePadding(context.getResources().getDimensionPixelSize(R.dimen.padding_toast));
https://stackoverflow.com/questions/7571917
复制相似问题