Adding AdMob Ad to RelativeLayout

偶尔善良 提交于 2020-01-01 17:11:02

问题


I have my app completely done. I want to add my admob ad to the bottom of my screen. I have the jar file imported and all that. I just cannot figure out how to get the ad at the bottom of the screen then what I need in the .java/main.xml/manifest.xml I tried a few tutorials but just got a force close.


回答1:


Have you found the following site from Admob?

http://code.google.com/mobile/ads/docs/android/fundamentals.html

This is a really good guide on how to integrate the sdk. It explains what must be declared in the manifest, layout and code itself. Be sure to follow the "create your banner in XML." Link on this page - this will show you how to setup your main xml. Where it states,

<com.google.ads.AdView android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="MY_AD_UNIT_ID"                          
ads:adSize="BANNER"                         
ads:loadAdOnCreate="true"/>

Simply add the tag,

android:layout_alignParentBottom="true" 

to position the ad at the bottom of the layout. So if your using a relative layout it will appear something like,

 <RelativeLayout
    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"
      ads:backgroundColor="#000000"
      ads:adUnitId="<Your Ad Unit ID>"
      ads:primaryTextColor="#FFFFFF"
      ads:secondaryTextColor="#CCCCCC"
      ads:adSize="BANNER"
    />
</RelativeLayout>

Because you are using RelativeLayout, replace the banner example code with,

 // Create the adView     
AdView adView = new AdView(this, AdSize.BANNER, MY_AD_UNIT_ID);     
// Lookup your RelativeLayoutLayout assuming it’s been given     
// the attribute android:id="@+id/ad"     
RelativeLayoutlayout = (RelativeLayout)findViewById(R.id.ad);     
// Add the adView to it
layout.addView(adView);     
// Initiate a generic request to load it with an ad     
adView.loadAd(new AdRequest());

Noticed, on a seperate note I think there is a typo on the Advanced tab for this site in the code sample at the InterstitialAd section of Advanced -

interstitialAd.loadAd(adRequest); 

should read,

interstitial.loadAd(adRequest);

Hope this helps



来源:https://stackoverflow.com/questions/6105673/adding-admob-ad-to-relativelayout

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!