2013-04-12 101 views
7

我已經用MonoGame開發了幾個月的遊戲,而且幾天前我剛剛發佈了它。現在,我試圖讓AdMob正常工作,以便我可以發佈免費的精簡版應用程序。我是Android佈局的新手,所以我不知道我在做什麼。如何在Xamarin/Monogame的Android應用程序屏幕底部顯示AdMob廣告?

在淘金網之後,我可以通過MonoGame實現AdMob,但應用始終顯示在屏幕的頂部,因此我能夠在我的應用中顯示廣告。我很幸運找到任何有關用MonoGame/Xamarin專門重新定位廣告的信息。

這裏是工作的代碼,並顯示在屏幕頂端的廣告:

Activity1.cs:

public class Activity1 : Microsoft.Xna.Framework.AndroidGameActivity 
    { 
     internal View adView = null; 

     protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 
      Game1.Activity = this; 
      var g = new Game1(); 
      FrameLayout fl = new FrameLayout(this); 
      fl.AddView(g.Window); 
      adView = AdMobHelper.CreateAdView(this, "<my id here>"); 
      var layoutParams = new ViewGroup.LayoutParams(Android.Views.ViewGroup.LayoutParams.FillParent, Android.Views.ViewGroup.LayoutParams.WrapContent); 

      fl.AddView(adView, layoutParams); 
      SetContentView(fl); 
      AdMobHelper.RequestFreshAd(adView); 
      g.Run(); 


     } 
    } 

這工作沒有任何額外的佈局。現在,這裏是我從涵蓋的話題幾個網站同化代碼:

Activity1.cs:

public class Activity1 : Microsoft.Xna.Framework.AndroidGameActivity 
    { 
     internal View adView = null; 

     protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 

      Game1.Activity = this; 
      var g = new Game1(); 
      SetContentView(Resource.Layout.Main); 
      View gameView = FindViewById(Resource.Id.GameView); 
      ViewGroup parent = (ViewGroup)gameView.Parent; 
      int index = parent.IndexOfChild(gameView); 
      parent.RemoveView(gameView); 
      parent.AddView(g.Window, index); 
      adView = FindViewById(Resource.Id.Ad); 
      parent = (ViewGroup)adView.Parent; 
      index = parent.IndexOfChild(adView); 
      parent.RemoveView(adView); 
      var layoutParams = adView.LayoutParameters; 
      adView = AdMobHelper.CreateAdView(this, "<my id here>"); 
      adView.LayoutParameters = layoutParams; 
      parent.AddView(adView, index); 
      AdMobHelper.RequestFreshAd(adView); 
      g.Run(); 
     } 
    } 

而我的另外Main.axml文件:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <SurfaceView 
     android:id="@+id/GameView" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 
    <com.google.ads.AdView 
     android:id="@+id/Ad" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_gravity="bottom" /> 
</FrameLayout> 

然而,當我運行這個時,我遇到了崩潰在「SetContentView(Resource.Layout.Main);」在Activity1.cs中。 Visual Studio給出了錯誤:

Unhandled Exception: 
Android.Views.InflateException: Binary XML file line #1: Error inflating class com.admob.android.ads.AdView 

雖然在錯誤輸出窗口中沒有任何錯誤來檢查...任何幫助?理想情況下,我想讓這個admob廣告展示在我的應用底部。謝謝!

回答

2

這是添加admob的代碼。我已經成功地完成,它可能爲你工作

<com.google.ads.AdView 
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
android:id="@+id/adView" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
ads:adSize="BANNER" 
ads:adUnitId="Your Admob 15 digit alpha numeric ID" 
/> 

,並在活動中的onCreate方法中添加以下代碼

AdView adview = (AdView)findViewById(R.id.adView); 
AdRequest re = new AdRequest(); 
re.setTesting(true); 
adview.loadAd(re); 

這是爲我工作。欲瞭解更多詳情,您可以訪問AdMob網站

1

解決方案#2
無需任何編碼一個快速的方法是剛剛的xmlns添加到應用程序元素和ads:loadAdOnCreate="true"adView元素AndroidManifest.xml中

0

這應該適合你,我用這種方式沒有問題。

public class Activity1 : Microsoft.Xna.Framework.AndroidGameActivity 
{ 
    internal View adView = null; 

    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 
     Game1.Activity = this; 
     var g = new Game1(); 
     FrameLayout fl = new FrameLayout(this); 
     LinearLayout ll = new LinearLayout(this); 
     ll.setOrientation(LinearLayout.VERTICAL); 
     ll.setGravity(Gravity.BOTTOM); 
     fl.AddView(g.Window); 
     adView = AdMobHelper.CreateAdView(this, "<my id here>"); 
     ll.addView(adView); 
     fl.AddView(ll); 
     SetContentView(fl); 
     AdMobHelper.RequestFreshAd(adView); 
     g.Run(); 


    } 
} 
相關問題