适用于不同设备的 Admob 横幅大小

Admob banner size for different devices(适用于不同设备的 Admob 横幅大小)
本文介绍了适用于不同设备的 Admob 横幅大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就在两天前,我才知道 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 横幅大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)