博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android Toast---五种情形
阅读量:4131 次
发布时间:2019-05-25

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

 Toast用于在手机屏幕上向用户显示一条信息,一段时间后信息会自动消失。信息可以是简单的文本,也可以是复杂的图片及其他内容(显示一个view)。

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

完整代码

1.Main,java

package com.wjq.toast;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.view.View.OnClickListener;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;import android.widget.Toast;public class Main extends Activity implements OnClickListener { Handler handler = new Handler(); @Override public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.main);  findViewById(R.id.btnSimpleToast).setOnClickListener(this);  findViewById(R.id.btnSimpleToastWithCustomPosition).setOnClickListener(    this);  findViewById(R.id.btnSimpleToastWithImage).setOnClickListener(this);  findViewById(R.id.btnCustomToast).setOnClickListener(this);  findViewById(R.id.btnRunToastFromOtherThread).setOnClickListener(this); } public void showToast() {  handler.post(new Runnable() {   @Override   public void run() {    Toast.makeText(getApplicationContext(), "我来自其他线程!",      Toast.LENGTH_SHORT).show();   }  }); } @Override public void onClick(View v) {  Toast toast = null;  switch (v.getId()) {  case R.id.btnSimpleToast:   Toast.makeText(getApplicationContext(), "默认Toast样式",     Toast.LENGTH_SHORT).show();   break;  case R.id.btnSimpleToastWithCustomPosition:   toast = Toast.makeText(getApplicationContext(),     "自定义位置Toast", Toast.LENGTH_LONG);   toast.setGravity(Gravity.CENTER, 0, 0);   toast.show();   break;  case R.id.btnSimpleToastWithImage:   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();   break;  case R.id.btnCustomToast:   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();   break;  case R.id.btnRunToastFromOtherThread:   new Thread(new Runnable() {    public void run() {     showToast();    }   }).start();   break;  } }}
2.main,xml
3.custom.xml

转自:

转载地址:http://kajvi.baihongyu.com/

你可能感兴趣的文章
idea的安装以及简单使用
查看>>
Windows mysql 安装
查看>>
python循环语句与C语言的区别
查看>>
Vue项目中使用img图片和background背景图的使用方法
查看>>
vue 项目中图片选择路径位置static 或 assets区别
查看>>
vue项目打包后无法运行报错空白页面
查看>>
Vue 解决部署到服务器后或者build之后Element UI图标不显示问题(404错误)
查看>>
element-ui全局自定义主题
查看>>
facebook库runtime.js
查看>>
vue2.* 中 使用socket.io
查看>>
openlayers安装引用
查看>>
js报错显示subString/subStr is not a function
查看>>
高德地图js API实现鼠标悬浮于点标记时弹出信息窗体显示详情,点击点标记放大地图操作
查看>>
初始化VUE项目报错
查看>>
vue项目使用安装sass
查看>>
HTTP和HttpServletRequest 要点
查看>>
在osg场景中使用GLSL语言——一个例子
查看>>
关于无线PCB中 中50欧姆的特性阻抗的注意事项
查看>>
Spring的单例模式源码小窥
查看>>
后台服务的变慢排查思路(轻量级应用服务器中测试)
查看>>