简体   繁体   English

Android将现有视图替换为admob广告

[英]Android replace an existing view with an admob ad

I'm implementing ads in my android app. 我正在Android应用中实施广告。 They work fine, but it takes them a few seconds to pop up which then resizes my layout elements. 它们可以正常工作,但是要花几秒钟才能弹出,然后重新调整布局元素的大小。 I want to have the layout elements pre-resized even before the ad is loaded so they don't shift once it is. 我想甚至在广告加载之前就对布局元素进行预先调整大小,以便它们一旦移动就不会移位。

Is there any way to replace an existing view (taking up the space where the ad would go) with the ad view once it's loaded? 加载广告视图后,有什么方法可以用广告视图替换现有视图(占用广告的空间)? Any other way to get the same effect? 还有其他方法可以达到相同的效果吗?

Thanks 谢谢

(Also, I'm adding the ad view programmatically, not through XML, though I could change that if need be) (此外,我通过编程方式添加了广告视图,而不是通过XML添加,尽管我可以根据需要进行更改)

Assuming you're using AdSize.BANNER , the AdView has 50dp height. 假设您使用的是AdSize.BANNER ,则AdView的高度为50dp。 You can incorporate the AdView in XML and give it 50dp height, or your can define a container layout in XML with 50dp height and add the AdView programmatically to the container layout. 您可以将AdView合并到XML中并赋予其50dp的高度,也可以在XML中定义高度为50dp的容器布局,然后以编程方式将AdView添加到容器布局中。

What I ended up doing is setting the adView's minimum height equal to 50 pixels multiplied times the density factor (1.5 for high, 1 for medium, .75 for low) since those values correspond to the ad height for banners. 我最后要做的是将adView的最小高度设置为等于50像素乘以密度系数(高为1.5,中为1,低为.75),因为这些值与横幅广告的高度相对应。 Works perfectly. 完美运作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM