gpt4 book ai didi

android - Millennial Media 横幅广告显示整个屏幕为白色

转载 作者:行者123 更新时间:2023-11-30 02:37:48 25 4
gpt4 key购买 nike

我正在使用 Millennial Media SDK 并为 mMedia Ad 编写一个 Cordova/PhoneGap 插件,它在 iOS 和视频广告上运行良好,但在 Android 上使用横幅广告时我遇到了一些问题。

如果我插入 MMAdView 作为 Cordova WebView 的兄弟 View (停靠在顶部或底部),横幅广告始终显示全屏空白,覆盖 WebView。

当我使用 getWidth()getMeasuredWidth() 编写日志来检查 MMAdView 的大小时,它总是返回 0 或 1 而不是实际大小!

和这个问题很相似,不同的是我用的是纯java代码,而不是XML布局文件。

AdMob mediated Millenial Media ad taking entire screen on refresh

看截图:

banner unexpected white screen

代码如下:

                adView = new MMAdView(cordova.getActivity());
adView.setApid( bannerAdId );
adView.setId(MMSDK.getDefaultAdId());

width = BANNER_AD_WIDTH;
height = BANNER_AD_HEIGHT;
//Finds an ad that best fits a users device.
if(canFit(MED_BANNER_WIDTH)) {
width = MED_BANNER_WIDTH;
height = MED_BANNER_HEIGHT;
} else if(canFit(IAB_LEADERBOARD_WIDTH)) {
width = IAB_LEADERBOARD_WIDTH;
height = IAB_LEADERBOARD_HEIGHT;
}
adView.setWidth(width);
adView.setHeight(height);

adView.setListener(new BannerListener());
adView.getAd();

ViewGroup parentView = (ViewGroup) webView.getParent();
if(argPos <= TOP_RIGHT) {
parentView.addView(adView, 0);
} else {
parentView.addView(adView);
}

最佳答案

经过研究,我自己找到了解决方案。

首先,创建横幅 View 并设置逻辑 adWidth 和 adHeight:

//Constants for tablet sized ads (728x90)
private static final int IAB_LEADERBOARD_WIDTH = 728;
private static final int IAB_LEADERBOARD_HEIGHT = 90;

private static final int MED_BANNER_WIDTH = 480;
private static final int MED_BANNER_HEIGHT = 60;

//Constants for phone sized ads (320x50)
private static final int BANNER_AD_WIDTH = 320;
private static final int BANNER_AD_HEIGHT = 50;

MMAdView ad = new MMAdView(getActivity());
ad.setApid( adId );
ad.setMMRequest(new MMRequest());
ad.setId(MMSDK.getDefaultAdId());
ad.setIgnoresDensityScaling(ignoreScaling);
ad.setTransitionType(transitionType);

//Finds an ad that best fits a users device.
if(canFit(MED_BANNER_WIDTH)) {
adWidth = MED_BANNER_WIDTH;
adHeight = MED_BANNER_HEIGHT;
} else if(canFit(IAB_LEADERBOARD_WIDTH)) {
adWidth = IAB_LEADERBOARD_WIDTH;
adHeight = IAB_LEADERBOARD_HEIGHT;
}
ad.setWidth(adWidth);
ad.setHeight(adHeight);

横幅 View 无法返回正确的宽度和高度,必须根据 adWidth 和 adHeight 计算。

    DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
float screenDensity = metrics.density;

int adViewWidth = (int) (adWidth * screenDensity);
int adViewHeight = (int) (adHeight * screenDensity);

然后,使用计算出的 adViewWidth 和 adViewHeight 设置横幅 View 的位置。

    adViewLayout = new RelativeLayout(activity);

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
ViewGroup rootView = (ViewGroup) getView().getRootView();
rootView.addView(adViewLayout, params);
adViewLayout.bringToFront();

Log.d(LOGTAG, String.format("show banner: (%d,%d), (%d x %d)", x,y,bw,bh));

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(bw, bh);
params.leftMargin = x;
params.topMargin = y;
adViewLayout.addView(adView, params);

现在可以了,看截图。

enter image description here

关于android - Millennial Media 横幅广告显示整个屏幕为白色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26229104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com