请选择 进入手机版 | 继续访问电脑版

牛大大的个人博客

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2453|回复: 1

Android Toast使用的简单小结

[复制链接]

149

主题

201

帖子

643

积分

版主

Rank: 7Rank: 7Rank: 7

积分
643
发表于 2020-5-31 20:45:40 | 显示全部楼层 |阅读模式
本帖最后由 牛大舅 于 2020-5-31 20:49 编辑

主要实现了几种常用的方式:
1.最基本的Toast
Toast.makeText(getApplicationContext(), "默认Toast样式", Toast.LENGTH_SHORT).show();


2.自定义位置的Toast

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

3.自定义布局(带图片)的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.ic_launcher);
toastView.addView(imageCodeProject, 0);
toast.show();


4.自定义带动画效果的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.ic_launcher);
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();



回复

使用道具 举报

149

主题

201

帖子

643

积分

版主

Rank: 7Rank: 7Rank: 7

积分
643
 楼主| 发表于 2021-1-4 14:34:56 | 显示全部楼层
Toast toast =Toast.makeText(getApplicationContext(),"请填写IP地址:192.168.0.1",Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM | Gravity.CENTER, 0, 0);
toast.show();

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|牛大大的个人博客 ( 苏ICP备17043571号-2 )

GMT+8, 2021-6-15 11:51 , Processed in 0.185674 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表