android中一些术语:XX的上下文,这个上下文怎么理解?
PHP中文网
PHP中文网 2017-04-17 14:39:20
0
2
585

public CompassView(Context context, AttributeSet attrs) {

    super(context, attrs);
    init();
}

例如我这一个,默认就是调用这个构造方法。 我自己写的一个view的构造方法。context表示activity的上下文,attrs表示你在xml中配置的属性,例如宽,高等

context表示activity的上下文,,,这个上下文是什么意思?用些作文的来理解不通啊...求大牛形象理解一下


与已有问题重复:如何理解android中的上下文对象(Context对象)?

申请关闭

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(2)
Ty80

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:

TextView textView = new TextView(this);
textview.setText(R.string.balabala);

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:

    public final void setText(int resid) {
        setText(getContext().getResources().getText(resid));
    }

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!