Android 自定义吐司膨胀根的意思

Android 自定义吐司膨胀根的意思,android,android-layout,layout-inflater,android-toast,Android,Android Layout,Layout Inflater,Android Toast,这是创建自定义toast的一行代码(from:) 它工作正常,但我不知道为什么要使用代码 LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup) findViewById(R.id.toast_layout_root)); TextView text = (TextView

这是创建自定义toast的一行代码(from:)

它工作正常,但我不知道为什么要使用代码

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
                           (ViewGroup) findViewById(R.id.toast_layout_root));

TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("This is a custom toast");

Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
我认为
findViewById
方法运行find-view“present”元素, 因此该方法返回null

这意味着,没有意义的代码,不是吗

为什么我想知道这个问题是我试图在服务中使用createcustomtoast

你知道,服务还没有找到ViewById

所以我只是试着膨胀(id,null),这会导致我的布局被破坏

我已经读过这篇文章了


如何在没有布局参数丢失信息的情况下正确充气

是否从android.R导入R?不是从android.R导入R,您可以在android中看到xml文件:id=“@+id/toast\u layout\u root”
(ViewGroup) findViewById(R.id.toast_layout_root)