分享Android中Toast的自定义使用

所属分类: 软件编程 / Android 阅读数: 1830
收藏 0 赞 0 分享

1.Toast源码分析

老规矩,我们先去看Toast的源码。

Toast有两种显示布局方式,一种最常见调用Toast.makeText()  ,看源码是这样写的

public static Toast makeText(Context context, CharSequence text, @Duration int duration) {
Toast result = new Toast(context);

LayoutInflater inflate = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
tv.setText(text);

result.mNextView = v;
result.mDuration = duration;

return result;
}

transient_notification这个布局文件代码是这样的

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="?android:attr/toastFrameBackground">

<TextView
android:id="@android:id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:textAppearance="@style/TextAppearance.Toast"
android:textColor="@color/bright_foreground_dark"
android:shadowColor="#BB000000"
android:shadowRadius="2.75"
/>

</LinearLayout>

那么我们想要修改Toast的文字消息样式,其实就是修改Toast根布局和message这个TextView。

Toast的另外一种显示模式就是自定义布局显示。这个方法不调用Toast.makeText()方法,而是new一个Toast对象,然后调用setView()方法。当然自定义布局就不会加载transient_notification布局了。

2.实现自定义Toast

先给大家看下我封装的工具类ToastUtil。

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by 赵晨璞 on 2016/8/11.
 */
public class ToastUtil {

private Toast toast;
private LinearLayout toastView;

/**
 * 修改原布局的Toast
 */
public ToastUtil() {

}

/**
 * 完全自定义布局Toast
 * @param context
 * @param view
 */
public ToastUtil(Context context, View view,int duration){
  toast=new Toast(context);
  toast.setView(view);
  toast.setDuration(duration);
}

/**
 * 向Toast中添加自定义view
 * @param view
 * @param postion
 * @return
 */
public ToastUtil addView(View view,int postion) {
  toastView = (LinearLayout) toast.getView();
  toastView.addView(view, postion);

  return this;
}

/**
 * 设置Toast字体及背景颜色
 * @param messageColor
 * @param backgroundColor
 * @return
 */
public ToastUtil setToastColor(int messageColor, int backgroundColor) {
  View view = toast.getView();
  if(view!=null){
    TextView message=((TextView) view.findViewById(android.R.id.message));
    message.setBackgroundColor(backgroundColor);
    message.setTextColor(messageColor);
  }
  return this;
}

/**
 * 设置Toast字体及背景
 * @param messageColor
 * @param background
 * @return
 */
public ToastUtil setToastBackground(int messageColor, int background) {
  View view = toast.getView();
  if(view!=null){
    TextView message=((TextView) view.findViewById(android.R.id.message));
    message.setBackgroundResource(background);
    message.setTextColor(messageColor);
  }
  return this;
}

/**
 * 短时间显示Toast
 */
public ToastUtil Short(Context context, CharSequence message){
  if(toast==null||(toastView!=null&&toastView.getChildCount()>1)){
    toast= Toast.makeText(context, message, Toast.LENGTH_SHORT);
    toastView=null;
  }else{
    toast.setText(message);
    toast.setDuration(Toast.LENGTH_SHORT);
  }
  return this;
}

/**
 * 短时间显示Toast
 */
public ToastUtil Short(Context context, int message) {
  if(toast==null||(toastView!=null&&toastView.getChildCount()>1)){
    toast= Toast.makeText(context, message, Toast.LENGTH_SHORT);
    toastView=null;
  }else{
    toast.setText(message);
    toast.setDuration(Toast.LENGTH_SHORT);
  }
 return this;
}

/**
 * 长时间显示Toast
 */
public ToastUtil Long(Context context, CharSequence message){
  if(toast==null||(toastView!=null&&toastView.getChildCount()>1)){
    toast= Toast.makeText(context, message, Toast.LENGTH_LONG);
    toastView=null;
  }else{
    toast.setText(message);
    toast.setDuration(Toast.LENGTH_LONG);
  }
  return this;
}

/**
 * 长时间显示Toast
 *
 * @param context
 * @param message
 */
public ToastUtil Long(Context context, int message) {
  if(toast==null||(toastView!=null&&toastView.getChildCount()>1)){
    toast= Toast.makeText(context, message, Toast.LENGTH_LONG);
    toastView=null;
  }else{
    toast.setText(message);
    toast.setDuration(Toast.LENGTH_LONG);
  }
  return this;
}

/**
 * 自定义显示Toast时间
 *
 * @param context
 * @param message
 * @param duration
 */
public ToastUtil Indefinite(Context context, CharSequence message, int duration) {
  if(toast==null||(toastView!=null&&toastView.getChildCount()>1)){
    toast= Toast.makeText(context, message,duration);
    toastView=null;
  }else{
    toast.setText(message);
    toast.setDuration(duration);
  }
   return this;
}

/**
 * 自定义显示Toast时间
 *
 * @param context
 * @param message
 * @param duration
 */
public ToastUtil Indefinite(Context context, int message, int duration) {
  if(toast==null||(toastView!=null&&toastView.getChildCount()>1)){
    toast= Toast.makeText(context, message,duration);
    toastView=null;
  }else{
    toast.setText(message);
    toast.setDuration(duration);
  }
  return this;
}

/**
 * 显示Toast
 * @return
 */
public ToastUtil show (){
  toast.show();

  return this;
}

/**
 * 获取Toast
 * @return
 */
public Toast getToast(){
  return toast;
}
}

修改Toast背景色的使用法方法如下:

ToastUtil toastUtil=new ToastUtil();
toastUtil.Short(MainActivity.this,"自定义message字体、背景色").setToastColor(Color.WHITE, getResources().getColor(R.color.colorAccent)).show();


修改Toast背景色

方形的Toast看上去有些呆板,我自定义了一个名为toast_radius.xml的背景,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 填充的颜色 -->
<solid android:color="#ffc107" />

<!-- android:radius 弧形的半径 -->
<corners android:radius="20dip" />

</shape>

然后上面设置背景的代码改成:

toastUtil.Short(MainActivity.this,"自定义message字体颜色和背景").setToastBackground(Color.WHITE,R.drawable.toast_radius).show();


修改了背景的Toast

虽然官方认为Toast和Snackbar都应该是短文本的形式,不能包含图标,但是个人感觉加上图标还是挺好玩的...

向Toast中添加图标可以这样:

 ImageView toastImage = new ImageView(getApplicationContext());
 toastImage.setImageResource(R.mipmap.ic_launcher);
 toastUtil.Short(MainActivity.this,"向Toast添加了一个ImageView").setToastBackground(Color.WHITE,R.drawable.toast_radius).addView(toastImage,0).show();


添加图标的Toast

如果你想要Toast显示自定义的布局,可以这样:

 View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.image,null);
 new ToastUtil(MainActivity.this,view,Toast.LENGTH_SHORT).show();


自定义布局Toast,我的布局文件中只有一个默认图标的ImageView

大家都知道,连续触发Toast的show()方法的时候,Toast就会排着队连续展示,感觉上不太友好。所以我先判断了toast是否没被创建或者是否被添加了额外的view,如果是的话就重新生成一个toast对象;如果否的话就只修改message文字和显示时间。


Toast布局修改,不排队显示

我这个工具类不是完全体,大家再根据自己项目的具体需求进行修改。以上就是Android中Toast的花式使用的全部内容,感兴趣的小伙伴们快快自己动手实践起来吧。

更多精彩内容其他人还在看

Android中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多