博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android Toast.makeText用法
阅读量:7065 次
发布时间:2019-06-28

本文共 1677 字,大约阅读时间需要 5 分钟。

Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且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();

 

custom.xml

 

  
    
    
      
      
    
  

 

转载于:https://www.cnblogs.com/duanjt/p/4571504.html

你可能感兴趣的文章
BSON与JSON的区别
查看>>
我的友情链接
查看>>
Play Framework 模板里使用注入访问数据层
查看>>
Win2008学习(十一),解决Remote App Web访问的证书问题
查看>>
python 实现 自动oa 签到签退 发送邮件提醒
查看>>
今天打开阿里妈妈惊现 ¥50 元佣金
查看>>
Oracle 正确删除archivelog文件
查看>>
微信JS 关闭网页
查看>>
[AAuto]给百宝箱增加娱乐功能
查看>>
【iOS开发】代码实现屏幕截图功能,也可以截取某个View 模糊效果
查看>>
SpringMVC 统一返回JSON格式数据到前端
查看>>
git push -u origin master报错src refspec master does
查看>>
nginx启动失败问题集锦
查看>>
性能测试分享:Jmeter的api监控工具解决方案
查看>>
老李分享:loadrunner 的86401错误
查看>>
一张图告诉你,只会jQuery还不够!
查看>>
ios中timer相关的延时调用需要注意的地方
查看>>
王者归来:GNOME 2回来
查看>>
Maven的安装配置
查看>>
存储过程3. 参数的引入
查看>>