【问题标题】:How to display AdMob ad at bottom of screen in Android app with Xamarin/Monogame?如何使用 Xamarin/Monogame 在 Android 应用的屏幕底部显示 AdMob 广告?
【发布时间】:2023-06-05 16:47:01
【问题描述】:

几个月前,我一直在用 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();


        }
    }

这无需任何额外的布局即可工作。现在,这是我从几个涵盖该主题的网站中吸收的代码:

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 广告显示在我的应用程序底部。谢谢!

【问题讨论】:

    标签: android admob xamarin monogame


    【解决方案1】:

    这是添加 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 网站

    【讨论】:

      【解决方案2】:

      解决方案 #2
      无需任何编码的快速方法是将 xmlns 添加到应用程序元素中,并将 ads:loadAdOnCreate="true" 添加到 AndroidManifest.xml 中的 adView 元素中

      【讨论】:

        【解决方案3】:

        这应该对你有用,我以这种方式使用它没有问题。

        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();
        
        
            }
        }
        

        【讨论】: