MonoGame を使用して数か月間ゲームを開発しており、数日前にリリースしたばかりです。現在、AdMob を正しく動作させて、無料のライト バージョンのアプリをリリースできるようにしています。私は Android レイアウトの初心者なので、自分が何をしているのかわかりません。
MonoGame を使用した AdMob の実装に関する情報を Web で探したところ、アプリ内に広告を表示することができましたが、アプリは常に画面の上部に表示されます。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();
}
}
これは、追加のレイアウトなしで機能します。さて、これがトピックをカバーするいくつかのWebサイトから吸収したコードです。
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 広告をアプリの下部に表示したいだけです。ありがとう!