以编程方式设置AdMob ID字符串 [英] Programatically set the AdMob id String
问题描述
我正在使用通用的 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 I
D 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>
我知道,与其实例化 AdMob
XML
,我可以通过编程方式实现,也可以将 main.xml
复制到每个项目并修改 ID
,而不是在我的图书馆中使用。但是,我希望使用与上述示例代码类似的解决方案,使我将 AdMob
代码保留在 main.xml
并且在发生变化时不会让我在n个地方更新 main.xml
。
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 ID
s 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设置一个空的占位符视图(此示例为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字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!