2013-04-12 38 views
7

Tôi đã phát triển trò chơi với MonoGame trong một vài tháng và tôi vừa phát hành nó một vài ngày trước. Bây giờ, tôi đang cố gắng làm cho AdMob hoạt động chính xác để tôi có thể phát hành phiên bản miễn phí của ứng dụng. Tôi là người mới đến bố cục Android, vì vậy tôi không biết mình đang làm gì.Làm cách nào để hiển thị quảng cáo AdMob ở cuối màn hình trong ứng dụng Android với Xamarin/Monogame?

Tôi có thể hiển thị quảng cáo trong ứng dụng của mình sau khi lướt web để biết một số thông tin về triển khai AdMob với MonoGame, nhưng ứng dụng luôn hiển thị trên đầu màn hình. Tôi đã có rất ít may mắn tìm thấy bất kỳ thông tin nào về việc định vị lại quảng cáo cụ thể với MonoGame/Xamarin.

Đây là mã mà làm việc, và hiển thị quảng cáo trên phía trên cùng của màn hình:

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


     } 
    } 

này hoạt động mà không cần bất kỳ bố trí bổ sung. Bây giờ, đây là đoạn code tôi đã đồng hóa từ một vài trang web bao gồm các chủ đề:

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

Và thêm tập tin Main.axml tôi:

<?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> 

Tuy nhiên, khi tôi chạy nó, tôi chạy vào một vụ tai nạn ở dòng "SetContentView (Resource.Layout.Main);" trong Activity1.cs. Visual Studio cung cấp cho các lỗi:

Unhandled Exception: 
Android.Views.InflateException: Binary XML file line #1: Error inflating class com.admob.android.ads.AdView 

Mặc dù không có bất kỳ lỗi nào trong cửa sổ đầu ra lỗi để kiểm tra ... Bất kỳ sự giúp đỡ? Lý tưởng nhất, tôi muốn chỉ nhận được quảng cáo admob này để hiển thị ở cuối ứng dụng của tôi. Cảm ơn!

Trả lời

2

Đây là mã để thêm admob. Tôi đã thực hiện thành công, nó có thể làm việc cho bạn

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

và trong hoạt động của bạn, trong phương pháp onCreate thêm mã sau đây

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

Đây là công việc cho tôi. Để cụ thể hơn, bạn có thể truy cập vào trang AdMob

1

Giải pháp # 2
Một cách nhanh chóng mà không cần bất kỳ mã hóa là chỉ cần thêm xmlns đến các yếu tố ứng dụng và ads:loadAdOnCreate="true" tới phần tử adView trong AndroidManifest.xml

0

Điều này sẽ phù hợp với bạn, tôi đang sử dụng nó theo cách này mà không gặp vấn đề gì.

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


    } 
}