@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/Text