我已经使用MonoGame开发了一个游戏数月,几天前刚刚发布。现在,我正在尝试正确地使用AdMob以便发布应用的免费精简版。我对Android布局是新手,所以我不知道该怎么做。
在通过搜索实现AdMob与MonoGame的信息后,我成功地让广告显示在我的应用中,但是该应用总是显示在屏幕顶部。我很难找到任何关于特定使用MonoGame/Xamarin重新定位广告的信息。
这里是能够工作并将广告显示在屏幕顶部的代码:
Activity1.cs:
然而,当我运行此代码时,在Activity1.cs文件中的"SetContentView(Resource.Layout.Main);"一行处出现了崩溃。Visual Studio 显示错误信息:
尽管错误输出窗口中没有任何错误可供检查... 有什么帮助吗?理想情况下,我希望能够在我的应用程序底部显示此Admob广告。谢谢!
在通过搜索实现AdMob与MonoGame的信息后,我成功地让广告显示在我的应用中,但是该应用总是显示在屏幕顶部。我很难找到任何关于特定使用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>
然而,当我运行此代码时,在Activity1.cs文件中的"SetContentView(Resource.Layout.Main);"一行处出现了崩溃。Visual Studio 显示错误信息:
Unhandled Exception:
Android.Views.InflateException: Binary XML file line #1: Error inflating class com.admob.android.ads.AdView
尽管错误输出窗口中没有任何错误可供检查... 有什么帮助吗?理想情况下,我希望能够在我的应用程序底部显示此Admob广告。谢谢!