自定義Android Toast

jopen 12年前發布 | 25K 次閱讀 Android Android開發 移動開發

Toast用于向用戶顯示一些幫助/提示。下面我做了4中效果,來說明Toast的強大,定義一個屬于你自己的Toast。

1.默認效果

自定義Android Toast

代碼

Toast.makeText(getApplicationContext(), "默認Toast樣式",
     Toast.LENGTH_SHORT).show();

2.自定義顯示位置效果

自定義Android Toast

代碼

toast = Toast.makeText(getApplicationContext(),
     "自定義位置Toast", Toast.LENGTH_LONG);
   toast.setGravity(Gravity.CENTER, 0, 0);
   toast.show();

3.帶圖片效果

自定義Android Toast

代碼

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.完全自定義效果

自定義Android Toast

代碼如下:

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();


 

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!