我已經用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廣告展示在我的應用底部。謝謝!
鏈接被破壞 –