代码也比较简单,拿到view之后只是设置了一下字体大小。
为什么这么写呢,且看接下来源码分析(非常简单)。
===============================================================
我们一般的调用是这么写的:
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()
一行代码,也很容易能找到重点——makeText
,没错,接下来从这里开始分析
compileSdkVersion 30之前
以compileSdkVersion 28
为例,makeText
源码:
public static Toast makeText(@NonNull Context context, @Nullable Looper looper,
@NonNull CharSequence text, @Duration int duration) {
Toast result = new Toast(context, looper);
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;
}
这几行的代码重点在哪呢,在这:
View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
引用了一个布局来显示信息
这个layout也非常的简单:
<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_marginHorizontal=“24dp”
android:layout_marginVertical=“15dp”
android:layout_gravity=“center_horizontal”
android:textAppearance=“@style/TextAppearance.Toast”
android:textColor=“@color/primary_text_default_materiaal_light”/>
根布局LinearLayout
和TextView
显示文本。
所以才有了前面报错的这行代码:
val linearLayout = toast!!.view as LinearLayout
现在看来其实是没有错的,事实上运行在Android11
以下也确实没问题。
setView
、getView
也是没问题的
/**
-
Set the view to show.
-
@see #getView
*/
public void setView(View view) {
mNextView = view;
}
/**
-
Return the view.
-
@see #setView
*/
public View getView() {
return mNextView;
}
author:yechaoa
compileSdkVersion 30之后
重点来了,在compileSdkVersion 30
之后,源码是有改动的
还是直接看重点makeText
:
public static Toast makeText(@NonNull Context context, @Nullable Looper looper,
@NonNull CharSequence text, @Duration int duration) {
if (Compatibility.isChangeEnabled(CHANGE_TEXT_TOASTS_IN_THE_SYSTEM)) {
Toast result = new Toast(context, looper);
result.mText = text;
result.mDuration = duration;
return result;
} else {
Toast result = new Toast(context, looper);
View v = ToastPresenter.getTextToastView(context, text);
result.mNextView = v;
result.mDuration = duration;
return result;
}
}
嗯?view的获取方式变了,原来是inflate
的方式,现在是
View v = ToastPresenter.getTextToastView(context, text);
ok,继续看ToastPresenter.getTextToastView
public class ToastPresenter {
…
@VisibleForTesting
public static final int TEXT_TOAST_LAYOUT = R.layout.transient_notification;
/**
- Returns the default text toast view for message {@code text}.
*/
public static View getTextToastView(Context context, CharSequence text) {
View view = LayoutInflater.from(context).inflate(TEXT_TOAST_LAYOUT, null);
TextView textView = view.findViewById(com.android.internal.R.id.message);
textView.setText(text);
return view;
}
}
到这里是不是有点熟悉了,没错,跟compileSdkVersion 28
中的源码差不多,但是layout变成常量
了,且有@VisibleForTesting
注解,不过xml
代码还是一样的。
而且setView
、getView
也弃用的
/**
-
Set the view to show.
-
@see #getView
-
@deprecated Custom toast views are deprecated. Apps can create a standard text toast with the
-
{@link #makeText(Context, CharSequence, int)} method, or use a
-
<a href="{@docRoot}reference/com/google/android/material/snackbar/Snackbar">Snackbar</a>
-
when in the foreground. Starting from Android {@link Build.VERSION_CODES#R}, apps
-
targeting API level {@link Build.VERSION_CODES#R} or higher that are in the background
-
will not have custom toast views displayed.
*/
@Deprecated
public void setView(View view) {
mNextView = view;
}
/**
-
Return the view.
-
Toasts constructed with {@link #Toast(Context)} that haven't called {@link #setView(View)}
-
with a non-{@code null} view will return {@code null} here.
-
Starting from Android {@link Build.VERSION_CODES#R}, in apps targeting API level {@link
-
Build.VERSION_CODES#R} or higher, toasts constructed with {@link #makeText(Context,
-
CharSequence, int)} or its variants will also return {@code null} here unless they had called
-
{@link #setView(View)} with a non-{@code null} view. If you want to be notified when the
-
toast is shown or hidden, use {@link #addCallback(Callback)}.
-
@see #setView
-
@deprecated Custom toast views are deprecated. Apps can create a standard text toast with the
-
{@link #makeText(Context, CharSequence, int)} method, or use a
-
<a href="{@docRoot}reference/com/google/android/material/snackbar/Snackbar">Snackbar</a>
-
when in the foreground. Starting from Android {@link Build.VERSION_CODES#R}, apps
-
targeting API level {@link Build.VERSION_CODES#R} or higher that are in the background
-
will not have custom toast views displayed.
*/
@Deprecated
@Nullable public View getView() {
return mNextView;
}
直接来看注释
的重点:
@deprecated Custom toast views are deprecated. Apps can create a standard text toast with the
{@link #makeText(Context, CharSequence, int)} method, or use a Snackbar
when in the foreground. Starting from Android {@link Build.VERSION_CODES#R}, apps
targeting API level {@link Build.VERSION_CODES#R} or higher that are in the background
will not have custom toast views displayed.
大意:
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
最后
其实Android开发的知识点就那么多,面试问来问去还是那么点东西。所以面试没有其他的诀窍,只看你对这些知识点准备的充分程度。so,出去面试时先看看自己复习到了哪个阶段就好。
下面分享的腾讯、头条、阿里、美团、字节跳动等公司2019-2021年的高频面试题全套解析,博主还把这些技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,下面只是以图片的形式给大家展示一部分。
知识不体系?这里还有整理出来的Android进阶学习的思维脑图,给大家参考一个方向。
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
其实Android开发的知识点就那么多,面试问来问去还是那么点东西。所以面试没有其他的诀窍,只看你对这些知识点准备的充分程度。so,出去面试时先看看自己复习到了哪个阶段就好。
下面分享的腾讯、头条、阿里、美团、字节跳动等公司2019-2021年的高频面试题全套解析,博主还把这些技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,下面只是以图片的形式给大家展示一部分。
[外链图片转存中…(img-Ph8sBw7F-1713710199294)]
知识不体系?这里还有整理出来的Android进阶学习的思维脑图,给大家参考一个方向。
[外链图片转存中…(img-V047eFsV-1713710199295)]
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!