toastutils报错_Android 自定义Toast,并且勘误Android工具类里面的ToastUtils-阿里云开发者社区...

前言

相信大部分仁兄在使用系统Toast的时候,都感觉不太尽如人意,因为系统Toast显示的位置比较固定,并且字体颜色等会跟随系统版本变化,那么能不能自己写一个呢,答案是当然的。当然,这是本文的一个重点,还有一个重点是,要对上一篇Android工具类里面的ToastUtils进行勘误,大概你现在还不太明白,容我慢慢道来,你且看着。

先上图

![系统Toast.png](http://upload-images.jianshu.io/upload_images/1716569-cf64bfc0bee87b5a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

自定义的不带图片的Toast(代码里编辑布局).png

自定义的带图片的Toast.png

首先

先看toast源码

public void setText(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);

}

如果不指定view,则mNextView的值为null。就会抛出异常。另外使用第一种方式时,就算你为Toast设置好了View也不能这样调用:toast.setText(s);

会抛出This Toast was not created with Toast.makeText()异常。解决的办法是定义一个TextView,然后为TextView赋值,再加到XXLayout中,

使用setView(XXLayout)把XXLayout设置成Toast的View。

本人遇到的错误我也一并贴出来

Caused by: java.lang.RuntimeException: This Toast was not created with Toast.makeText()

at android.widget.Toast.setText(Toast.java:275)

at cn.and.ToastActivity.onCreate(ToastActivity.java:20)

at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

再看看Android 工具类里面的ToastUtils写法

public static void showToast(Context context, String message) {

if (toast == null) {

toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);

} else {

toast.setText(message);

}

toast.show();

}

虽然平时用着不会报错,但是有的时候还是会报错,为了防患于未然&#x

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 中,可以通过自定义 Toast 实现类似于悬浮通知的效果。具体步骤如下: 1. 创建一个自定义Toast 布局文件,例如 `custom_toast.xml`,可以在其中添加需要展示的内容,比如标题、内容、图标等。 2. 在代码中创建 Toast 对象,并将其设置为自定义的布局: ```java LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup) findViewById(R.id.custom_toast_container)); Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show(); ``` 3. 如果需要实现悬浮通知的效果,可以将 Toast 的位置设置为 `Gravity.TOP | Gravity.LEFT`,并通过定时器不断更新 Toast 的位置。具体代码可以参考下面的示例: ```java final WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, PixelFormat.TRANSLUCENT); params.gravity = Gravity.TOP | Gravity.LEFT; params.x = 0; params.y = 100; final Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.TOP | Gravity.LEFT, 0, 0); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); final Handler handler = new Handler(); final Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { handler.post(new Runnable() { @Override public void run() { params.y -= 10; toast.getView().setAlpha(toast.getView().getAlpha() - 0.1f); WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE); windowManager.updateViewLayout(toast.getView(), params); if(params.y <= 0) { timer.cancel(); } } }); } }, 0, 100); ``` 注意:为了实现悬浮通知的效果,需要在 AndroidManifest.xml 文件中添加以下权限: ```xml <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值