当前位置: 移动技术网 > 移动技术>移动开发>Android > 分享Android中Toast的自定义使用

分享Android中Toast的自定义使用

2019年07月24日  | 移动技术网移动技术  | 我要评论

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的花式使用的全部内容,感兴趣的小伙伴们快快自己动手实践起来吧。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网