AdMob 如何确定何时分配了 SMART_BANNER 大小 [英] AdMob how to determine when SMART_BANNER size has been allocated
问题描述
我有一个 Activity,其布局包括一个带有 ads:adSize="SMART_BANNER"
的 AdView
.它位于屏幕底部,在自定义 WebView
下方.现在,WebView
具有根据可用空间(像素尺寸)呈现的内容.因此,在渲染 WebView
内容之前,我需要知道 SMART_BANNER
将占用的大小(特别是高度),以及因此为 WebView 留下的内容代码>.这里 指出智能横幅的高度可以是任何
32dp
、50dp
和 90dp
之一.
I have an Activity with a layout that includes an AdView
with ads:adSize="SMART_BANNER"
. This sits at the bottom of the screen, below a custom WebView
. Now, the WebView
has content that is rendered based on the space (pixel dimensions) available to it. So before I render the WebView
content I need to know what size (height in particular) the SMART_BANNER
will be taking up, and what is therefore left for the WebView
. From here it is stated that the smart banner can have a height that is any one of 32dp
, 50dp
and 90dp
.
据我所知,我需要等待 AdView
的格式确定,然后才能呈现 WebView
.我怎样才能做到这一点?也许通过 设置 AdListener?但是, 的方法AdListener
似乎只允许我确定例如当一个广告已经加载...我不想等待广告加载,我只想等待分配广告的实际大小.
So far as I can understand, I need to wait for the format of the AdView
to be decided before I can render the WebView
. How can I do that? Maybe by setting an AdListener? However, the methods of AdListener
only seem to allow me to determine e.g. when an ad has been loaded... I don't want to have to wait for the ad to be loaded, I only want to wait for the actual size of the ad to be allocated.
我知道我可以通过将 AdView
设置为 BANNER
来避免这一切,固定高度为 50dp
,这确实有效美好的.但我更喜欢 SMART_BANNER
主要是因为在小手机屏幕上横向模式下高度可以缩小到 32dp
,为实际内容留出更多空间.
I know I can avoid all this by setting the AdView
to BANNER
, with a fixed height of 50dp
, and this does indeed work fine. But I would prefer SMART_BANNER
mainly because the height can shrink to 32dp
in landscape mode on a small phone screen, leaving more room for actual content.
推荐答案
AdView
没有 API 来在 Ad 实际加载之前检索其大小并且类是 final
,所以你也不能自己实现它.
AdView
has no API to retrieve its size before the Ad has actually loaded and the class is final
, so you cannot implement it yourself either.
要动态确定横幅高度,您需要使用您怀疑的 AdListener
.
To determine the banner height dynamically you need to use an AdListener
as you suspected.
另一种替代方法是使用动态选择的静态值(来自文档)根据设备的屏幕高度:
Another alternative is to use static values (from the documentation) chosen dynamically according to the screen height of the device:
32 dp,其中高度 ≤ 400 dp
32 dp where height ≤ 400 dp
50 dp,其中高度 > 400 dp 且 ≤ 720 dp
50 dp where height > 400 dp and ≤ 720 dp
90 dp 其中高度 > 720 dp
90 dp where height > 720 dp
(您可以查看这个答案以确定屏幕高度和这个 用于在 px 和 dp 值之间转换)
(you can check this answer for determining the screen height and this one for converting between px and dp values)
例如,您可以在 Activity
中使用类似的内容:
For example you could use something like this in your Activity
:
private int getSmartBannerHeightDp() {
DisplayMetrics dm = getResources().getDisplayMetrics();
float screenHeightDp = dm.heightPixels / dm.density;
return screenHeightDp > 720 ? 90 : screenHeightDp > 400 ? 50 : 32;
}
这篇关于AdMob 如何确定何时分配了 SMART_BANNER 大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!