我真的很喜欢编程。我已经按照Libgdx和谷歌的教程, 但我无法在我的应用程序中使用admob制作横幅广告。 有人可以帮助我教我应该做些什么来将admob横幅广告放入我的应用程序中吗?
谢谢,
答案 0 :(得分:0)
您需要创建一个View以包含adMob View和Libgdx View,然后将setContentView()设置为此布局,您必须将两个Views添加到其中。 this是您所引用的教程吗?这非常有帮助。
答案 1 :(得分:0)
在你的android-android项目里面打开Activity并创建一个全屏的相对布局,没有标题栏 -
RelativeLayout layout=new RelativeLayout(this);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
现在低于此更改此行 -
initialize(new Mygame(), cfg);
到
View gameView=initializeForView(new MyGame() , cfg);
此方法 initializeForView(new MyGame(),cfg); 返回一个视图,该视图将保存为将添加到相对布局的视图实例。现在创建广告也将添加到相对布局为 -
Adview adView=new AdView(this);// create new adview instance
adView.setAdSize(com.google.android.gms.ads.AdSize.BANNER);// set the adsize
adView.setAdUnitId("XXXXXXXXX");// your adunit id
//Create layout params for adview
RelativeLayout.LayoutParams adParams=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.ALIGN_PARENT_RIGHT);//set position
//Add both gameview and adview to the relative layout-
layout.addView(gameView);
layout.addView(adView, adParams);
//Now just request ad by creating new instance of AdRequest
AdRequest request=new AdRequest.Builder().build();//.addTestDevice(AdRequest.DEVICE_ID_EMULATOR) to add test device
adView.loadAd(request);
// At last setcontent view to the Relative Layout that has both gameView and adView.
setContentView(layout);
您还需要覆盖onPause,onResume和onDestroy,以处理广告的暂停,恢复或失效。
@Override
protected void onResume() {
super.onResume();
if(adView!=null){
adView.resume();
}
}
@Override
protected void onPause() {
super.onPause();
if(adView!=null){
adView.pause();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(adView!=null){
adView.destroy();
}
}
此处adView已添加到您的游戏中,但是如果您想要处理何时显示adView。您可能希望在游戏运行时隐藏adView并在游戏结束时显示,这可以通过创建处理程序来实现。< / p>