Programatically set the AdMob id String(以编程方式设置 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 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>
我知道不是从 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 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 设置一个空的占位符 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 字符串


- android 4中的android RadioButton问题 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01