/**
-
Show the view or text notification for a long period of time. This time
-
could be user-definable.
*/
public static final int LENGTH_LONG = 1;
private Toast() {
}
/**
-
Make a standard toast that just contains a text view.
-
@param context The context to use. Usually your {@link android.app.Application}
-
or {@link android.app.Activity} object. -
@param text The text to show. Can be formatted text.
-
@param duration How long to display the message. Either {@link #LENGTH_SHORT} or
-
{@link #LENGTH_LONG}
*/
public static android.widget.Toast makeText(Context context, CharSequence text, @Duration int duration) {
android.widget.Toast result = new android.widget.Toast(context);
LayoutInflater inflate = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflate.inflate(R.layout.transient_notification, null);
TextView tv = (TextView) v.findViewById(R.id.message);
tv.setText(text);
result.setView(v);
result.setDuration(duration);
return result;
}
/**
-
Make a standard toast that just contains a text view with the text from a resource.
-
@param context The context to use. Usually your {@link android.app.Application}
-
or {@link android.app.Activity} object. -
@param resId The resource id of the string resource to use. Can be formatted text.
-
@param duration How long to display the message. Either {@link #LENGTH_SHORT} or
-
{@link #LENGTH_LONG} -
@throws Resources.NotFoundException if the resource can’t be found.
*/
public static android.widget.Toast makeText(Context context, @StringRes int resId, @Duration int duration)
throws Resources.NotFoundException {
return makeText(context, context.getResources().getText(resId), duration);
}
}
核心是makeText(Context context, CharSequence text, @Duration int duration)方法,仍然是创建系统Toast,关键点是加载我们自己的transient_notification.xml布局文件,细心的朋友可能还会看到:处理根布局View和duration两个变量,我们分别使用系统Toast提供的setView和setDuration两个public方法。
transient_notification.xml
<LinearLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:background=“@android:drawable/toast_frame”
android:gravity=“center”
android:orientation=“vertical”>
<TextView
android:id=“@+id/message”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_gravity=“center”
android:drawableLeft=“@mipmap/qq”
android:drawableRight=“@mipmap/qq”
android:shadowColor=“#BB000000”
android:shadowRadius=“2.75”
android:textAppearance=“@style/TextAppearance.Toast”
android:textColor=“#1bb5d7”/>
需要什么样式的Toast,就定义自己的transient_notification.xml布局就可以,显示图片还是文字完全由你掌控!
我们的Toast定义完了,怎么用呢?
Toast.makeText(MainActivity.this, “自定义toast”, Toast.LENGTH_LONG).show();
看到这行代码有没有很亲切!没错,我们的自定义Toast就是这么用的!
只有一点不同,不要再导入系统的Toast类了,用自己定义的,连包都不需要导入了!
import android.widget.Toast;//删掉这句,无需导包
尾声
最后,我再重复一次,如果你想成为一个优秀的 Android 开发人员,请集中精力,对基础和重要的事情做深度研究。
对于很多初中级Android工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。 整理的这些架构技术希望对Android开发的朋友们有所参考以及少走弯路,本文的重点是你有没有收获与成长,其余的都不重要,希望读者们能谨记这一点。
这里,笔者分享一份从架构哲学的层面来剖析的视频及资料分享给大家梳理了多年的架构经验,筹备近6个月最新录制的,相信这份视频能给你带来不一样的启发、收获。

Android进阶学习资料库
一共十个专题,包括了Android进阶所有学习资料,Android进阶视频,Flutter,java基础,kotlin,NDK模块,计算机网络,数据结构与算法,微信小程序,面试题解析,framework源码!

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!
er,java基础,kotlin,NDK模块,计算机网络,数据结构与算法,微信小程序,面试题解析,framework源码!
[外链图片转存中…(img-kzOv9H8e-1715363481945)]
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!


921

被折叠的 条评论
为什么被折叠?



