google 播放服务的 admob 未在表面视图上显示

admob of google play service does not showing over surfaceview

我有一个 android 游戏,它在 surfaceView 上绘制,我在上面使用 FrameLayout 制作了 admob 广告。 FrameLayout lay = new FrameLayout(this); this.addContentView(lay, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

    AdView adView = new AdView(this, AdSize.BANNER, "xxxxxxxx");
    lay.addView(renderer, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    lay.addView(adView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    adView.setGravity(Gravity.LEFT);
    this.setContentView(lay);
    adView.loadAd(new AdRequest());

一切都很好。我想集成新的 google play services admob,我已将上面的代码更改为

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 中,那么我可以毫无问题地看到广告

有人遇到过这个问题吗?有人对此有什么建议吗?

国王问候

确保为广告视图设置背景颜色。可以是"transparent"。然后,使用 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);

}