问题描述
就在两天前,我才知道 SMART_BANNER 并不是最好的 CTR,我们应该在 admob 的广告尺寸之间动态切换.
Just two days back, I came to know that SMART_BANNER is not the best for good CTR and we should dynamically switch between ad sizes for admob.
这是我编写的 Java 代码.当我在 4 英寸模拟器上运行代码时,我看到请求了 728x90 的广告并且响应是无效的广告尺寸.(错误描述是广告不适合当前屏幕)请.帮助:
Here is the Java code, I have written. When I ran the code on a 4inch emulator, I see that 728x90 ad is requested and the response is invalid ad size. (description of error is that ad won't fit the current screen) pls. help:
AdSize adsize = AdSize.SMART_BANNER;
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
int orientation = display.getOrientation();
if(width >= 728 && height >= 90 ) {
adsize = AdSize.IAB_LEADERBOARD;
System.out.println("728 x 90");
} else if (width >= 468 && height >= 60 ) {
adsize = AdSize.IAB_BANNER;
System.out.println("468 x 60");
} else if (width >= 320 && height >= 50 ) {
adsize = AdSize.BANNER;
System.out.println("320 x 50");
}
LinearLayout adContainer = (LinearLayout) findViewById(R.id.cakes);
adView = new AdView(this, adsize, "xxxxxxxxxx");
AdRequest adRequest = new AdRequest();
adView.loadAd(adRequest);
// Place the ad view.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
adContainer.addView(adView, params);
推荐答案
getWindowManager().getDefaultDisplay().getWidth()
返回宽度(以像素为单位).您需要根据与密度无关的像素宽度来决定显示哪个横幅.
getWindowManager().getDefaultDisplay().getWidth()
returns the width in pixels. You need to be making a decision on which banner to show based upon the width in density independent pixels.
我很久以前就决定最好的解决方案是使用 Android 资源配置来指定 adBannerSize.例如
I decided a long time ago that the best solution was to use Android resource config to specify the adBannerSize. Eg
final AdSize adSize;
final int adBannerSize = getResources().getInteger(R.integer.adBannerSize);
switch (adBannerSize) {
case 1 :
adSize = AdSize.BANNER;
break;
case 2 :
adSize = AdSize.IAB_BANNER;
break;
case 3 :
adSize = AdSize.IAB_LEADERBOARD;
break;
default:
Log.w(TAG, "No AdSize specified");
adSize = AdSize.BANNER;
break;
}
然后您可以轻松配置广告横幅大小以匹配您打算支持的任何设备配置.
Then you can easily configure the ad banner size to match whatever device configuration you intend to support.
这篇关于适用于不同设备的 Admob 横幅大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!