如何在Fragment上调用getBaseContext()

hec*_*ana 8 android android-context android-fragments

我有这个方法,可以从正常活动调用:

getBaseContext().getResources().updateConfiguration(config2, getBaseContext().getResources().getDisplayMetrics());
Run Code Online (Sandbox Code Playgroud)

问题是我不能getBaseContext()在Fragment中调用,我该怎么做?或者我需要getBaseContext()从Fragment类中替换什么?

Ner*_*een 31

您可以使用getActivity(),它返回与片段关联的活动.
活动是一个上下文(因为Activity扩展了Context).

但是你可以在片段附加到活动后使用它.


ing*_*sid 10

使用 getActivity().getBaseContext();