开发者

Don't understand the constructor of my View

My code is working and so, but I don't understand one thing. I've started game programming and now works my whole SurfaceView and so on. I have draw a background so it all works.

When I created this I followed a tutorial, so know I'm commenting on my own language. :) When I now started to comment some things; I can't get rid of this, can someone tell me why my constructor for my view has the Context parameter and AttributeSet paramater? What makes it necessary to have these and why do I need to set the localContext value to my GameView context?

Game

public class开发者_StackOverflow中文版 Game extends Activity{

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(new GameView(this, null));
}

}

GameView

    public GameView(Context localContext, AttributeSet attrSet){
    super(localContext, attrSet);
    context=localContext;
    InitView();
}

The InitView(); just initialize every objects and variables I created; if this stuff is needed in order to explain the constructor-thing to me; I will post it.

Thanks!


The Context carries a bunch of state with it that Views use to operate. This includes things like configuration state to help the resource system determine which resources to use among a number of others.

The AttributeSet is used when your view is inflated from an XML layout. This is how XML attributes get bound to view properties during layout inflation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜