如何在libgdx中植入广告

如何在libgdx中植入广告

摘自http://code.google.com/p/libgdx/wiki/AdMobInLibgdx

libgdx初始化的方式一般如下:

public class HelloWorldAndroid extends AndroidApplication { 

   
@Override public void onCreate (Bundle savedInstanceState) { 

       
super.onCreate(savedInstanceState); 

        initialize
(new HelloWorld(), false);             

   
} 

}

initialize中封装了初始化view:

       requestWindowFeature(Window.FEATURE_NO_TITLE); 

       getWindow
().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 

       getWindow
().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); 

       setContentView
(graphics.getView(), createLayoutParams()); 

为了将广告的view植入进去,不能使用initialize,使用initializeForView,这里以多盟为例如下:

 

public class HelloWorldAndroid extends AndroidApplication { 

   
@Override public void onCreate (Bundle savedInstanceState) { 

       
super.onCreate(savedInstanceState); 

        //initialize
(new HelloWorld(), false);             

        // Create the layout
        RelativeLayout layout = new RelativeLayout(this);
 
        // Do the stuff that initialize() would do for you
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        // Create the libgdx View
        View gameView = initializeForView(new RabbitGame(this), false);

        // Add the libgdx view
        layout.addView(gameView);
       

        //初始化广告视图
        DomobAdView mAdview320x50 = new DomobAdView(this, "56OJyM1ouMGoaSnvCK", DomobAdView.INLINE_SIZE_320X50);

        RelativeLayout.LayoutParams adParams = 
            new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                            RelativeLayout.LayoutParams.WRAP_CONTENT);
        adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        adParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
   
       
        layout.addView(mAdview320x50, adParams);
        // Hook it all up
        setContentView(layout);


    } 

}

 

你可能感兴趣的:(如何在libgdx中植入广告)