Android中Toast的几种使用方法

时间:浏览526次 发布人:繼續乄梛傷

Android中Toast的几种使用方法

已解决问题

谷歌繼續乄梛傷用户在浏览526次提交了关于“红旗l7Android中Toast的几种使用方法”的提问,欢迎大家涌跃发表自己的观点。目前共有1个回答,最后更新于2024-07-24T12:59:00。希望大家能够帮助她。

详细问题描述及疑问:期待您的答案,谢了,下次有事儿别忘了找我 !

希望以下的回答,能够帮助你。

第1个回答

用户名:lcrlee  

Toast是Andro建听烈要要般妒id中用来显示显示信息的一种机制,和来自Dialog不一样的是,Toast是没有焦点的,而且问答Toast显示的时间有限,过一定的时间就会自动消失。有几种使用方式:
1、结于夜支合该松房掉绍径默认效果代码
Toast.m**eText(getApplicationContext(),"默认Toa讨影复景条台委拉st样式",Toast.LENGTh**_Sh**ORT).show();
2、自定义显示位置效果代码
toast=Toast.m**eText(getApplicationContext(),
"自定义位置Toast",Toas甚生阿王皮她呀非仅略困t.LENGTh**_LONG);
toast令就乐击紧客够.setGr**ity(***.CENTER,0,0);
***.show();
3、带图片效果代码
toast=Toast.m**eText(g衡倒也etApplicationContext(),
"带图片的Toast",Toast.LENGTh**_LONG);
toast.setG防r**ity(***.CENTER,0,让晶吗物半越贵景认血0);
LinearLayouttoastView=(LinearLayout)toast.getView();
排使父米灯ImageViewimageCodeProject=newImage吧只View(getApplic对ationContext());
imageCodeProject.setImageResource(R.drawable.icon);
toastView.addView让(imageCodeProject,0);
***.show();
4、完全自师味量绿厚定义效果
代码
LayoutInflaterinflater=getLayoutInflater();
Viewlayout=犯而跑兴也教面此京掉留inflater.inflate(R.layout.custom,
(ViewGroup)findViewById(R.id.llToast));
ImageViewimage=(ImageView)layout
.findViewB乐起沉宣威马几功次需向yId(R.id.tvImageToast);
image.setImageResource(R.drawable.icon);
TextViewtitle=(TextView)layout.findViewById(R.id.tvTitleToast);
title.setTe背xt("Attention");
TextViewtext=(TextView)layout.findViewById(R.id.tvTextToast);
text.setText("完全自定义Toast");
toast=newToa保风st(getApplicationContext());
toast.setGr**ity(Gr**ity.RIGh**T|***.TOP,12,40);
toast.setDuration(Toast.LENGTh**_LONG);
toast.setView(layout);
***.show();
5.其他线程
代码
newThread(newRunnable(){
publicvoidrun(){
showToast();
}
}).start();