android custom toast,ToastCustom【自定义显示风格的Toast】

版权声明:本文为博主原创文章,未经博主允许不得转载。

前言

基于系统Toast的自定义显示风格的Toast。

效果图

6ee2162a9393ca880d47ceb3f5e776f2.png

代码分析

ToastCustom类基于系统Toast,不是继承Toast,只是通过toast.setView(view)方法引用自定义的显示风格布局文件,达到自定义显示风格的目的。

为了和Toast用法保持一致,ToastCustom类中也使用了makeText、show、setGravity、setText方法。方便在项目中直接替换Toast。

下面分析下ToastCustom类中的setText()方法

该方法用来修改显示的文本,刚开始的时候,我直接使用了toast.setText()方法进行修改文本:

public voidsetText(CharSequence s){

toast.setText(s);

}

但是程序崩溃了。分析原因得知toast的setText方法是找到系统Toast的系统布局文件mNextView中的ID值为message的TextView控件,然后修改这个TextView控件的文本实现的。(下面是源码)

/*** Update the text in a Toast that was previously created using one of the makeText() methods.

*@params The new text for the Toast.*/

public voidsetText(CharSequence s) {if (mNextView == null) {throw new RuntimeException("This Toast was not created with Toast.makeText()");

}

TextView tv=(TextView) mNextView.findViewById(com.android.internal.R.id.message);if (tv == null) {throw new RuntimeException("This Toast was not created with Toast.makeText()");

}

tv.setText(s);

}

但是在ToastCustom类中我们已经修改了toast的布局文件引用,所以直接使用toast.setText()方法的时候,肯定找不到ID值为message的TextView控件。正确的代码如下:

public voidsetText(CharSequence s){

TextView tv=(TextView) toast.getView().findViewById(R.id.tv_toast);

tv.setText(s);

}

使用步骤

一、项目组织结构图

033a9b889ee77c1069faa70393b5704c.png

注意事项:

1、 导入类文件后需要change包名以及重新import R文件路径

2、 Values目录下的文件(strings.xml、dimens.xml、colors.xml等),如果项目中存在,则复制里面的内容,不要整个覆盖

二、导入步骤

将ToastCustom复制到项目中,并重新import R文件

2b65ef29a5872cc0e4771c25889edd04.gif

6a087676c59fa8b19d76e6bb55a32902.gif

packagecom.why.project.toastcustom.views;importandroid.content.Context;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.widget.TextView;importandroid.widget.Toast;importcom.why.project.toastcustom.R;/*** Create By HaiyuKing

* Used 自定义Toast显示风格,基于系统Toast【可以控制显示样式、位置,不可以控制显示时间、动画,不可触发】

* 注意 Toast布局在源码中的布局是采用LinearLayout*/

public classToastCustom {private staticToastCustom toastCustom;privateToast toast;public static ToastCustom makeText(Context context, CharSequence text, intduration){

LayoutInflater inflate=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View view= inflate.inflate(R.layout.toast_custom_view, null);

TextView tv=(TextView)view.findViewById(R.id.tv_toast);

tv.setText(text);if (toastCustom == null) {

toastCustom= newToastCustom();

}

toastCustom.toast= newToast(context);

toastCustom.toast.setView(view);

toastCustom.toast.setDuration(duration);returntoastCustom;

}public static ToastCustom makeText(Context context, int resId, intduration){returnToastCustom.makeText(context,context.getResources().getString(resId),duration);

}public voidshow(){

toast.show();

}/*** 1、gravity是输入Toast需要显示的位置,例如CENTER_VERTICAL(垂直居中)、CENTER_HORIZONTAL(水平居中)、TOP(顶部)等等。

* 2、xOffset则是决定Toast在水平方向(x轴)的偏移量,偏移量单位为,大于0向右偏移,小于0向左偏移

* 3、yOffset决定Toast在垂直方向(y轴)的偏移量,大于0向下偏移,小于0向上偏移,想设大值也没关系,反正Toast不会跑出屏幕。*/

public void setGravity(int gravity, int xOffset, intyOffset) {

toast.setGravity(gravity, xOffset, yOffset);

}public voidsetText(CharSequence s){

TextView tv=(TextView) toast.getView().findViewById(R.id.tv_toast);

tv.setText(s);

}public void setText(intresId){

TextView tv=(TextView) toast.getView().findViewById(R.id.tv_toast);

tv.setText(resId);

}

}

ToastCustom.java

将toast_custom_view.xml文件复制到项目中

2b65ef29a5872cc0e4771c25889edd04.gif

6a087676c59fa8b19d76e6bb55a32902.gif

toast_custom_view.xml

将toast_custom_view_bg.xml文件复制到项目中

2b65ef29a5872cc0e4771c25889edd04.gif

6a087676c59fa8b19d76e6bb55a32902.gif

toast_custom_view_bg.xml

在dimens.xml中添加以下颜色标记的代码

16dp

16dp

18sp

10dp

20dp

30dp

三、使用方法

ToastCustom toastCustom = ToastCustom.makeText(this,"自定义Toast显示风格",Toast.LENGTH_LONG);

toastCustom.show();

如果想要修改文本或者显示位置,参考下面的代码:

ToastCustom toastCustom = ToastCustom.makeText(this,"自定义Toast显示风格",Toast.LENGTH_LONG);

toastCustom.setText(R.string.app_name);

toastCustom.setGravity(Gravity.CENTER,0,0);

toastCustom.show();

混淆配置

参考资料

android 自定义Toast显示风格

Android:谈一谈安卓应用中的Toast情节(基础)

Android:剖析源码,随心所欲控制Toast显示

项目demo下载地址

原文:http://www.cnblogs.com/whycxb/p/6830610.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值