2014-11-22 191 views
0

問題是,在540x960分辨率(我的手機)上,廣告效果不錯,但在其他解決方案中效果不佳。這是一個錯誤:「沒有足夠的空間來展示廣告」。我認爲這可能是錯誤的佈局,但我不知道是什麼。AdMob libgdx。廣告未顯示

我的MainActivity:

super.onCreate(savedInstanceState); 

     AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();   
     //initialize(new JumpJackieJump(this), cfg);   

     this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
     //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

     RelativeLayout layout = new RelativeLayout(this);   
     requestWindowFeature(Window.FEATURE_NO_TITLE); //no title is needed   

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

     adView = new AdView(this); 
     adView.setAdUnitId(ADCODE); 
     adView.setAdSize(AdSize.SMART_BANNER); 

     ad = new InterstitialAd(this); 
     ad.setAdUnitId(FULLADCODE); 

     View gameView = initializeForView(new JumpJackieJump(new RequestHandler(adView, ad), this), cfg);   

     AdRequest adRequest = new AdRequest.Builder().build(); 
     AdRequest fadReq = new AdRequest.Builder().build();   

     adView.loadAd(adRequest);   
     ad.loadAd(fadReq);   

     layout.setPadding(0, 0, 0, 0); 
     layout.addView(gameView);   

     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(adView, adParams);    

     setContentView(layout);  

     if (!ad.isLoaded()) 
      ad.loadAd(fadReq);   

     ad.setAdListener(new AdListener() 
     { 
      @Override 
      public void onAdClosed() 
      { 
      AdRequest fadReq = new AdRequest.Builder().build(); 
      ad.loadAd(fadReq); 
      } 
     }); 
+0

做的解決方案的工作? – 2014-12-01 09:19:25

+0

是的,它的工作;)謝謝你的答案。 – metalcracker 2014-12-01 16:15:00

回答

0

您可以動態加載adSize時基於該設備的屏幕分辨率:

 AdSize adSize = AdSize.SMART_BANNER; 

     DisplayMetrics dm = getResources().getDisplayMetrics(); 
     double density = dm.density * 160; 
     double x = Math.pow(dm.widthPixels/density, 2); 
     double y = Math.pow(dm.heightPixels/density, 2); 
     double screenInches = Math.sqrt(x + y); 
     if (screenInches > 8) { // > 728 X 90 
      adSize = AdSize.LEADERBOARD; 
     } else if (screenInches > 6) { // > 468 X 60 
      adSize = AdSize.MEDIUM_RECTANGLE; 
     } else { // > 320 X 50 
      adSize = AdSize.BANNER; 
     } 

     adView = new AdView(this); 
     adView.setAdUnitId(FULLADCODE); 
     adView.setAdSize(adSize); 
     AdRequest adRequest = new AdRequest.Builder().build(); 
     adView.loadAd(adRequest); 
+0

感謝您的回答,我會嘗試您的代碼,但是插頁式廣告有什麼用處? – metalcracker 2014-11-23 09:20:53

+0

據我所知,您無法控制插頁式廣告的尺寸。您只能設置要在admob控制檯中顯示的廣告類型 – 2014-11-23 10:46:03