android - 从 getContext() 或从构造函数获取上下文作为字段

标签 android android-custom-view android-context

我们以自定义或扩展View为例。

将构造函数中的 Context 参数保存为字段是否比在任何地方调用 getContext() 更有效(假设有 10 个或更多地方它是必需的)?

最佳答案

与其在任何地方都使用 getContext(),不如将当前上下文作为参数传递到您想要使用的构造函数中。

关于android - 从 getContext() 或从构造函数获取上下文作为字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10302477/

相关文章:

android - 为什么扩展 ImageView 标记为错误?

android - YouTube 安卓 API : YouTubePlayerFragment loading spinner

android - 从自定义 View 中检索继承的属性

android - 在 Recycler GridLayoutManager 中的 3 行和 4 行项目之间提供更多间距

java - 警报对话框中出现错误

android-activity - 在非 Activity 类中获取当前 Activity 上下文

Android 可扩展 ListView : set background color of selected item on click

java - 重启设备后如何使用bootreciever设置壁纸? (安卓, eclipse )

android - 无样式 ViewPagerIndicator 与 SherlockActionBar 结合使用

android - 如何通过 xml 为自定义 View 传递重力属性?