将 AdMob 广告添加到 RelativeLayout

Adding AdMob Ad to RelativeLayout(将 AdMob 广告添加到 RelativeLayout)
本文介绍了将 AdMob 广告添加到 RelativeLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序已完全完成.我想将我的 admob 广告添加到屏幕底部.我已经导入了 jar 文件等等.我只是不知道如何在屏幕底部获取广告,然后我在 .java/main.xml/manifest.xml 中需要什么我尝试了一些教程,但只是强制关闭.

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.

推荐答案

您从 Admob 找到以下网站了吗?

Have you found the following site from Admob?

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

这是一个关于如何集成 sdk 的非常好的指南.它解释了必须在清单、布局和代码本身中声明的内容.请务必遵循在 XML 中创建横幅".此页面上的链接 - 这将向您展示如何设置您的主 xml.在它声明的地方,

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

只需添加标签,

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>

因为您使用的是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());

注意到,在单独的说明中,我认为在高级的 InterstitialAd 部分的代码示例中,此站点的高级选项卡上有错字 -

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

应该阅读,

interstitial.loadAd(adRequest);

希望对你有帮助

这篇关于将 AdMob 广告添加到 RelativeLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)