Android Holo Light styling changes depending on chosen context

前端 未结 1 824
名媛妹妹
名媛妹妹 2021-01-12 02:41

I have been trying to find the cause of weird formatting style inconsistencies for my Views throughout my application and I think I have narrowed it down with t

相关标签:
1条回答
  • 2021-01-12 03:45

    In Android sources getApplicationContext() returns Application object which extends ContextWrapper, so using getApplicationContext() you are passing ContextWrapper subclass, but when you pass this, you are passing an Activity object which extends ContextThemeWrapper so you are passing ContextThemeWrapper subclass. Now the difference between ContextWrapper and ContextThemeWrapper is that ContextWrapper simply delegates all of its calls to another Context and ContextThemeWrapper allows you to modify the theme from what is in the wrapped context.

    Although, the question was more about why exactly this is happening (as opposed to the cause which was clear), here are also some helpful posts which explain the perils of incorrectly using application context and how to choose a context correctly:

    • When to call activity context OR application context?
    • Using Application context everywhere?

    Most importantly from @CommonsWare: "getApplicationContext() is not a complete Context and consequently does not support everything that Activity does."

    Awesome post about Context that should clarify everything:

    • http://www.doubleencore.com/2013/06/context/
    0 讨论(0)
提交回复
热议问题