分享Android中Toast的自定义使用

分享Android中Toast的自定义使用,第1张

概述1.Toast源码分析老规矩,我们先去看Toast的源码。Toast有两种显示布局方式,一种最常见调用Toast.makeText() ,看源码是这样写的

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"><TextVIEwandroID: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,Toast.LENGTH_SHORT);    toastVIEw=null;  }else{    toast.setText(message);    toast.setDuration(Toast.LENGTH_SHORT);  } return this;}/** * 长时间显示Toast */public ToastUtil Long(Context context,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,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,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,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"?><shapexmlns: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中Toast的自定义使用全部内容,希望文章能够帮你解决分享Android中Toast的自定义使用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1148553.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存