如何在Android应用中使用Xamarin/Monogame在屏幕底部显示AdMob广告?

DaM*_*lan 8 android admob monogame xamarin

我用MonoGame开发了几个月的游戏,几天前我刚刚发布了它.现在,我正在尝试让AdMob正常工作,这样我就可以发布应用程序的免费精简版.我是Android布局的新手,所以我不知道我在做什么.

在网上搜索了有关使用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();


        }
    }
Run Code Online (Sandbox Code Playgroud)

这没有任何额外的布局.现在,这里是我从几个涉及该主题的网站吸收的代码:

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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

还有我的额外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>
Run Code Online (Sandbox Code Playgroud)

但是,当我运行它时,我在"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
Run Code Online (Sandbox Code Playgroud)

虽然错误输出窗口中没有任何错误可以检查...有什么帮助吗?理想情况下,我想在我的应用程序底部显示这个admob广告.谢谢!

Und*_*und 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"
/>
Run Code Online (Sandbox Code Playgroud)

并在您的活动中,在 onCreate 方法中添加以下代码

AdView adview = (AdView)findViewById(R.id.adView);
AdRequest re = new AdRequest();
re.setTesting(true);
adview.loadAd(re);
Run Code Online (Sandbox Code Playgroud)

这对我来说是工作。欲了解更多详情,您可以访问 admob 网站