Admob的SMART_BANNER大小大小、Admob、SMART_BANNER

2023-09-08 15:52:22 作者:被淹死的鱼

该AdSize类有getWidthInPixels()和getHeightInPixels()方法来获取广告的大小。虽然,他们的工作正确的旗帜,他们不工作SMART_BANNER。他们总是返回-2。

The AdSize class has getWidthInPixels() and getHeightInPixels() methods to obtain size of the ad. Although, they work correctly for BANNER, they don't work SMART_BANNER. They always return -2.

能否请您给我建议的方式在运行时获取AD浏览报大小?

Can you please suggest me a way to fetch AdView size during run-time?

推荐答案

如果您使用的是谷歌Play业务库,你可以简单地使用:

If you're using the Google Play services library, you can simply use:

int widthPixels = AdSize.SMART_BANNER.getWidthInPixels(this);
int heightPixels = AdSize.SMART_BANNER.getHeightInPixels(this);

在旧的独立的Andr​​oid SDK AdMob的,你必须要做到这一点哈克的方式:

In the old standalone Android AdMob SDK, you have to do it the hacky way:

免责声明:这是不正确的方法来创建一个AdSize。 不通过这个AdSize到AD浏览报构造!的

Disclaimer: This is the incorrect way to create an AdSize. Do NOT pass this AdSize into the AdView constructor!

希望聪明的横幅执行将被固定在未来的版本,所以你不必为此哈克的解决方法。但在这里是如何可以做到的:

Hopefully the smart banner implementation will be fixed in future versions so you don't have to do this hacky workaround. But here is how it can be done:

// This testSize should not be passed to the AdView constructor.
// Always pass AdSize.SMART_BANNER instead.
AdSize testSize = AdSize.createAdSize(AdSize.SMART_BANNER, this);
int widthPixels = testSize.getWidthInPixels(this);
int heightPixels = testSize.getHeightInPixels(this);
// testSize should not be referenced past this point.
 
精彩推荐
图片推荐