AdSize クラスには、広告のサイズを取得するための getWidthInPixels() および getHeightInPixels() メソッドがあります。ただし、BANNER では正しく機能しますが、SMART_BANNER では機能しません。それらは常に -2 を返します。
実行時に AdView のサイズを取得する方法を教えてください。
Google Play サービス ライブラリを使用している場合は、次のように簡単に使用できます。
int widthPixels = AdSize.SMART_BANNER.getWidthInPixels(this);
int heightPixels = AdSize.SMART_BANNER.getHeightInPixels(this);
古いスタンドアロンの Android AdMob SDK では、ハックな方法で行う必要があります。
免責事項: これは AdSize を作成するための間違った方法です。この AdSize を AdView コンストラクタに渡さないでください。
うまくいけば、スマート バナーの実装は将来のバージョンで修正されるので、このハックな回避策を実行する必要はありません。しかし、これを行う方法は次のとおりです。
// 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.