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;
}
这几行的代码重点在哪呢,在这:<

1050

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



