以编程方式设置 AdMob id 字符串

Programatically set the AdMob id String(以编程方式设置 AdMob id 字符串)

本文介绍了以编程方式设置 AdMob id 字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发几个使用通用 XML 界面元素的应用程序.其中一个元素会加载 AdMob.我可以在每个项目中复制元素并将每个应用程序的唯一 ID 放在复制的 XML 文件中,但我想知道是否有某种方法可以以编程方式设置 I来自应用程序的 D 字符串,同时保留 XML 而不必在每个项目中复制它.

I have several apps I am working on which use common XML interface elements. One of these elements loads AdMob. I can duplicate the element in each project and put the unique ID for each app in the duplicated XML file but I am wondering if there is some way to programatically set the ID string from the application while keeping the XML and not having to duplicate it in each project.

类似于:

    setContentView(R.layout.main);
    AdView aView = (AdView)findViewById(R.id.admob);
    aView.<method goes here that sets ads:adUnitId>

我知道不是从 XML 实例化 AdMob,而是可以通过编程方式进行,或者我可以将 main.xml 复制到每个项目并在那里修改 ID,而不是使用我的库中的那个.但是我会更喜欢类似于我上面的示例代码的解决方案,它可以让我将 AdMob 代码保留在 main.xml 中并且不会让我更新 main.xml在n个地方发生变化.

I know that instead of instantiating AdMob from the XML, I can do that programatically, or that I can copy main.xml to each project and modify the IDs there, rather than using the one in my library. But I would be happier with a solution similar to my sample code above which lets me keep the AdMob code in main.xml and doesn't make me update main.xml in n places when it changes.

我无法找到 AdMob 附带的可交付成果的类参考或完整描述,并且自动完成没有向我显示任何似乎适合此需求的方法.如果有人能指点我一个对 AdMob 类有很好描述的地方,那将是一个很大的帮助.

I have been unable to find a class reference or complete description for the deliverables that come with AdMob, and auto complete does not show me any method which would seem to fit this need. If anybody would point me to a place which does have a good description of the AdMob classes, that would be a great help.

推荐答案

在您的 XML 中为 AdView 设置一个空的占位符 View(此示例为 ListView),然后在 Java 中以编程方式添加 AdView,如下所示:

Set an empty place holder View (ListView for this example) for the AdView in your XML, and then add the AdView programmatically in Java, something like:

ListView lv = (ListView)findViewById(R.id.adplaceholder);

//create the AdView (replace MY_BANNER_UNIT_ID with the admob ID of your choice)
AdView av = new AdView(this, AdSize.BANNER, MY_BANNER_UNIT_ID);
lv.addView(av);//add the AdView to your layout

AdRequest request = new AdRequest();
adView.loadAd(request); 

这篇关于以编程方式设置 AdMob id 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:以编程方式设置 AdMob id 字符串

基础教程推荐