Android 11适配指南之Toast解析

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;

}

这几行的代码重点在哪呢,在这:<

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值