Android google play服务的admob未在surfaceview上显示
我有一款基于surfaceView的android游戏,上面有admob广告,使用FrameLayout。 FrameLayout lay=新的FrameLayout(本); this.addContentView(lay,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)) 一切都很好。我想集成新的google play服务admob,我已将上述代码更改为Android google play服务的admob未在surfaceview上显示,android,android-layout,admob,google-play-services,surfaceview,Android,Android Layout,Admob,Google Play Services,Surfaceview,我有一款基于surfaceView的android游戏,上面有admob广告,使用FrameLayout。 FrameLayout lay=新的FrameLayout(本); this.addContentView(lay,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)) 一切都很好。我想集成新的google play服务admob,我已将上述代码更改为 FrameLayout lay = new Frame
FrameLayout lay = new FrameLayout(this);
this.addContentView(lay, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
adView = new AdView(this);
adView.setAdUnitId("xxxxxxx");
adView.setAdSize(AdSize.BANNER);
lay.addView(adView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
this.setContentView(lay);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
admob视图未显示。我知道这是没有出现的视图,因为如果我只将AdView添加到frameLayour中,那么我看到的广告没有任何问题
有人遇到过这个问题吗?有人对此有什么建议吗
king问候确保为adview设置背景色。它可以是“透明的”。然后,使用RelativeLayout或FrameLayout作为父布局,定义要定位的adView的布局参数,例如在屏幕的底部中心,如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
adView.setBackgroundColor(Color.TRANSPARENT);
// Add the AdView to the view hierarchy. The view will have no size
// until the ad is loaded.
RelativeLayout layout = new RelativeLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
// Create an ad request.
AdRequest adRequest = new AdRequest.Builder().build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
//Request full screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//Create a displayMetrics object to get pixel width and height
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
width = metrics.widthPixels;
height = metrics.heightPixels;
RelativeLayout.LayoutParams adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
layout.addView(renderer);
layout.addView(adView, adParams);
//Set main renderer
setContentView(layout);
}
非常感谢当我设置颜色时一切都很好。我想这是一个“黑客”,但我不介意。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
adView.setBackgroundColor(Color.TRANSPARENT);
// Add the AdView to the view hierarchy. The view will have no size
// until the ad is loaded.
RelativeLayout layout = new RelativeLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
// Create an ad request.
AdRequest adRequest = new AdRequest.Builder().build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
//Request full screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//Create a displayMetrics object to get pixel width and height
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
width = metrics.widthPixels;
height = metrics.heightPixels;
RelativeLayout.LayoutParams adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
layout.addView(renderer);
layout.addView(adView, adParams);
//Set main renderer
setContentView(layout);
}