【问题标题】:admob of google play service does not showing over surfaceview谷歌播放服务的 admob 没有显示在 Surfaceview 上
【发布时间】:2015-05-09 16:20:03
【问题描述】:

我有一个在surfaceView 上绘制的android 游戏,并且我在上面使用FrameLayout 有admob 广告。 FrameLayout 布局 = 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 中,那么我可以毫无问题地看到广告

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

国王的问候

【问题讨论】:

    标签: android android-layout admob google-play-services surfaceview


    【解决方案1】:

    确保为广告视图设置了背景颜色。它可以是“透明的”。然后,使用 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);
    
    }
    

    【讨论】:

    • 非常感谢我设置了颜色,一切都很好。我认为它是“黑客”,但我不介意。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-18
    • 2014-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多