public CompassView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
例如我这一个,默认就是调用这个构造方法。 我自己写的一个view的构造方法。context表示activity的上下文,attrs表示你在xml中配置的属性,例如宽,高等
context表示activity的上下文,,,这个上下文是什么意思?用些作文的来理解不通啊...求大牛形象理解一下
申请关闭
Context is used to access global information. For example, the constructor of each UI component must pass in a Context.
Take TextView as an example:
When instantiating here, a this is passed in, and this value is a Context.
Here is a string predefined in the strings.xml file. The source code of the setText(int resid) function is as follows:
Note that the getContext() function is used to obtain global information resources and filter out the resources you want by ID. The context obtained by getContext() here is the Context we passed in when instantiating TextView.
So, Context is used to assist objects in accessing global information.
Pay special attention when using it. The Context is passed out and released in time after use, because the Context has a reference to capture the Activity. If the Context is not released in time after the Activity life cycle ends, it will be very serious. It is easy to cause memory leaks.
Current execution environment.
function a(){
...
...
...
}
The context of function a is all the code inside the function. Or understood as the environment of a.