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

9
我已经使用MonoGame开发了一个游戏数月,几天前刚刚发布。现在,我正在尝试正确地使用AdMob以便发布应用的免费精简版。我对Android布局是新手,所以我不知道该怎么做。
在通过搜索实现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广告。谢谢!
3个回答

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"
/>

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

AdView adview = (AdView)findViewById(R.id.adView);
AdRequest re = new AdRequest();
re.setTesting(true);
adview.loadAd(re);

这对我来说是一项工作。如果需要更多细节,您可以访问admob网站。


1
这应该适用于您,我正在以这种方式使用它,没有任何问题。
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();


    }
}

1

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接