Android中Toast的用法简介

Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失。下面用一个实例来看看如何使用Toast。

1.默认效果

Android中Toast的具体使用_第1张图片

代码

Toast.makeText(getApplicationContext(), "默认Toast样式",
Toast.LENGTH_SHORT).show();

2.自定义显示位置效果

Android中Toast的具体使用_第2张图片

代码

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

3.带图片效果

Android中Toast的具体使用_第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.完全自定义效果

Android中Toast的具体使用_第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.其他线程

Android中Toast的具体使用_第5张图片

代码

new Thread(new Runnable() {
public void run() {
showToast();
}
}).start();

更多相关文章

  1. Android:如何从堆栈中还原ProGuard混淆后的代码
  2. Android实现选择相册图片并显示功能
  3. 运用android的Matrix类来旋转图片
  4. titanium更改默认图片
  5. android回收图片使用内存
  6. Android之获取手机上的图片和视频缩略图thumbnails
  7. Android加载SD卡目录,文件夹遍历,图片设置,设置文件对应打开方式等
  8. Android 通过蓝牙控制小车源代码+视频
  9. 73、ImageView 让图片填充满

随机推荐

  1. android 脸部抠图
  2. Android图片圆角处理
  3. Android 图片缩放,手势,事件
  4. [置顶] android AIDL 进程间通信
  5. Android 项目混编flutter报错
  6. android 实时获取网速
  7. Android之常用功能代码
  8. Android中xml生成
  9. Android下BroadcastReceiver应用详解
  10. Android(安卓)音频的播放之一