如何获得当前的背景?

Sud*_*esh 30 android

我想在我的数组适配器中使用它.当我放入this子活动来创建适配器时,它不起作用.

Dur*_*rga 35

getApplicationContext() 可以获取上下文值

  • 这将返回应用程序上下文,而不是当前上下文,两者都是不同的。 (2认同)

Aba*_*art 26

你可以通过几种方式获得上下文:

通过Activity,使用 Your_Activity_Name.this

通过申请,使用 getApplicationContext()

通过视图,使用 Your_View.getContext()

我不推荐的唯一一个是使用getBaseContext().如果您需要通用的东西,请在主活动中使用公共静态变量,并在应用启动时为其分配应用程序上下文.这样你就可以随时打电话了Your_Activity.your_context_variable


Mud*_*sir 3

你可以使用getBaseContext(),但这是not a local context.