消息模式Toast.makeText用法
Toast用於向用戶顯示一些幫助/提示。下面我做了5中效果,來說明Toast的強大,定義一個屬於你自己的Toast。
1.默認效果
代碼
Toast.makeText(getApplicationContext(), "默認Toast樣式",
Toast.LENGTH_SHORT).show();
2.自定義顯示位置效果
代碼
toast = Toast.makeText(getApplicationContext(),
"自定義位置Toast", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
3.帶圖片效果
代碼
toast = Toast.makeText(getApplicationContext(),
"帶圖片的Toast", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView =(LinearLayout) toast.getView();
ImageView imageCodeProject =new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.icon);
toastView.addView(imageCodeProject, 0);
toast.show();
4.完全自定義效果
代碼
LayoutInflater inflater = getLayoutInflater();
View layout =inflater.inflate(R.layout.custom,
(ViewGroup) findViewById(R.id.llToast));
ImageView image = (ImageView)layout
.findViewById(R.id.tvImageToast);
image.setImageResource(R.drawable.icon);
TextView title = (TextView)layout.findViewById(R.id.tvTitleToast);
title.setText("Attention");
TextView text = (TextView)layout.findViewById(R.id.tvTextToast);
text.setText("完全自定義Toast");
toast = newToast(getApplicationContext());
toast.setGravity(Gravity.RIGHT| Gravity.TOP, 12, 40);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
5.其他線程
代碼
new Thread(new Runnable() {
public voidrun() {
showToast();
}
}).start();
- (2011-06-15 14:31:45)
- (2011-06-14 10:06:37)
- (2011-06-10 09:40:41)
- (2011-06-09 17:07:01)
- (2011-06-09 13:52:59)
- (2011-06-08 17:25:38)
- (2011-05-27 14:34:37)
- (2011-05-27 11:50:35)
- (2011-05-26 17:15:31)
- (2011-05-26 15:01:53)