Android特效 五种Toast具体解释
? Toast是Android中用來顯示顯示信息的一種機制,和Dialog不一樣的是,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 = new Toast(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 void run() {
? ???showToast();
? ? }
? ?}).start();
轉自http://android.tgbus.com/Android/tutorial/201103/346236.shtml
總結
以上是生活随笔為你收集整理的Android特效 五种Toast具体解释的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 31Exchange Server 20
- 下一篇: iOS 技术官方 QA