问题
I am having a hard time integrating an Admob banner into my game. I have used simple-xml-2.6.2.jar library because I considered it to be simple to implement into my game. Now I have a single layout file, with the correct code for publisher ID and test device:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.ads.AdView android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="xxxxxxxxxxx"
ads:adSize="BANNER"
ads:testDevices="TEST_EMULATOR, zzzzzzzzzzzz"
ads:loadAdOnCreate="true"/>
</LinearLayout>
The game runs in landscape mode, and I would like tu get the ads to show as a banner at the top or bottom, but there aren't any test ads being shown, and I haven't got any ideas as to why is that. Any help is apreciated!
回答1:
Use below code in xml say xml name is adview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/adView"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="50dip"/>
Now in your Activity load the ad.
AdView adView = new AdView(this,AdSize.BANNER,"yourid");
LinearLayout ll= (LinearLayout) findViewById(R.id.adView);
ll.addView(adView);
AdRequest ar = new AdRequest();
ar.setGender(AdRequest.FEMALE);
adView.loadAd(ar);
You can include this view(xml) in any of your activity from xml like this..
<include layout="@layout/adview" /> // your xml
回答2:
I have 2 possible ideas for you:
- Delete the "zzzzzzzzzz" from the xml.
or
- Make sure you are using SDK 3.2 or above. I recently solved a similar problem with this method.
回答3:
Try this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.google.ads.AdView
android:id="@+id/ad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
ads:adSize="BANNER"
ads:adUnitId="@string/admob_publisher_id"
ads:loadAdOnCreate="true" >
</com.google.ads.AdView>
</RelativeLayout>
AdView adView = (AdView) findViewById(R.id.ad);
AdRequest adRequest = new AdRequest();
adView.loadAd(adRequest);
来源:https://stackoverflow.com/questions/13440803/admob-integration-with-simple-xml-in-android