Android 11适配指南之Toast解析(1),面试必要的几个问题

且看已知条件:

  • targetSdkVersion 30

  • Android 11

  • 小米10

文末附Android 11适配手册

定位问题

===============================================================

ok,遇到问题,迅速定位。

我在原有的Toast调用上重新封装了一下,即ToastUtil

所以很快就定位到问题所在了

private fun createToast(msg: String) {

if (toast == null) {

toast = Toast.makeText(YUtils.getApp().applicationContext, msg, Toast.LENGTH_SHORT)

} else {

toast!!.setText(msg)

}

val linearLayout = toast!!.view as LinearLayout

val messageTextView = linearLayout.getChildAt(0) as TextView

messageTextView.textSize = 15f

toast!!.show()

}

没错,就是这句进行了转换:

val linearLayout = toast!!.view as LinearLayout

代码也比较简单,拿到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”/>

根布局LinearLayoutTextView显示文本。

所以才有了前面报错的这行代码:

val linearLayout = toast!!.view as LinearLayout

现在看来其实是没有错的,事实上运行在Android11以下也确实没问题。

setViewgetView也是没问题的

/**

  • 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}.

*/

最后

Android学习是一条漫长的道路,我们要学习的东西不仅仅只有表面的 技术,还要深入底层,弄明白下面的 原理,只有这样,我们才能够提高自己的竞争力,在当今这个竞争激烈的世界里立足。

人生不可能一帆风顺,有高峰自然有低谷,要相信,那些打不倒我们的,终将使我们更强大,要做自己的摆渡人。

资源持续更新中,欢迎大家一起学习和探讨。

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值