Android 五种 Toast显示样式

一、系统默认Toast显示样式

Toast.makeText(this, "系统默认Toast显示方式", Toast.LENGTH_SHORT).show();

二、自定义Toast显示位置

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

三、自定义Toast带图片显示效果

Toast toast = Toast.makeText(this, "带图片的Toast", Toast.LENGTH_LONG);toast.setGravity(Gravity.CENTER, 0, 0);LinearLayout toastView = (LinearLayout) toast.getView();ImageView imgBack = new ImageView(this);imgBack.setImageResource(R.mipmap.ic_launcher);toastView.addView(imgBack, 0);toast.show();

四、完全自定义显示效果

LayoutInflater inflater = getLayoutInflater();View layout = inflater.inflate(R.layout.custom_toast_layout, (LinearLayout) findViewById(R.id.custom_toast_lay));ImageView image = (ImageView) layout.findViewById(R.id.custom_toast_img);image.setImageResource(R.mipmap.ic_launcher);TextView title = (TextView) layout.findViewById(R.id.custom_toast_title);title.setText("Attention");TextView text = (TextView) layout.findViewById(R.id.custom_toast_txt);text.setText("完全自定义Toast");Toast toast = new Toast(this);toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);toast.setDuration(Toast.LENGTH_LONG);toast.setView(layout);toast.show();

五、其他线程Toast显示

new Thread(newRunnable() {    public void run() {        Looper.prepare();        showToast();        Looper.loop();    }}).start();public void showToast () {    Toast.makeText(this, "其他线程", Toast.LENGTH_SHORT).show();}

注:不建议使用Looper,最好是能handler回主线程显示Toast

更多相关文章

  1. Android(安卓)调用地图显示信息
  2. Android(安卓)自定义角标
  3. android 2.3 webkit
  4. Android(安卓)系统应用调用,intent 的使用方法总结
  5. Android(安卓)Http基础:图片下载并显示和WebView的应用
  6. Android(安卓)Studio常用对话框
  7. 实现 Android(安卓)通知提示功能
  8. Android中的消息机制
  9. 面试官:作为Android高级攻城狮,请你解释一下 android:text 到 Text

随机推荐

  1. android 播放器开发
  2. android adapter的性能小结
  3. 首批Android(安卓)Q版本真机上线,Testin云
  4. 2011-03-29 15:30 android用于打开各种文
  5. 使用Animation轮播图片
  6. Android应用程序组件Content Provider的
  7. Android之——通用安装apk方法
  8. Android(安卓) 资源类型
  9. FFmpeg In Android(安卓)- H264码流解码/
  10. 学习Android书籍