Android 在片段中显示带有progressdialog的资源
我可以用三种不同的方式显示资源Android 在片段中显示带有progressdialog的资源,android,android-fragments,Android,Android Fragments,我可以用三种不同的方式显示资源 pDialog.setMessage(getActivity().getApplicationContext().getResources().getString(R.string.please_wait)); pDialog.setMessage(getContext().getResources().getString(R.string.please_wait)); pDialog.setMessage(getResources().getString(R
pDialog.setMessage(getActivity().getApplicationContext().getResources().getString(R.string.please_wait));
pDialog.setMessage(getContext().getResources().getString(R.string.please_wait));
pDialog.setMessage(getResources().getString(R.string.please_wait));
哪一条是正确的道路?为什么?没有正确的道路。您只需要上下文
:
当您在getResources()
类中时(或Activity
当您在Activity类中从内部类调用时)MyActivity.this.getResources()
(甚至getActivity().getResources()
)当您在getContext().getResources()
类中时Fragment
当您通过参数传递context.getResources()
时context
当您从view.getContext().getResources()
视图中获取
时Context
以下两个具有“等效的
上下文
”,因为您处于片段
中,就像我可以通过您的问号假设的那样:
pDialog.setMessage(getContext().getResources().getString(R.string.please_wait));
pDialog.setMessage(getResources().getString(R.string.please_wait));
在哪里
您可以获得entrie应用程序的上下文
这里有一些参考:
pDialog.setMessage(getActivity().getApplicationContext().getResources().getString(R.string.please_wait));