我正在使用Millennial Media SDK并为mMedia Ad编写Cordova / PhoneGap插件,它在iOS和视频广告上运行良好,但在Android上使用横幅广告时遇到了一些麻烦。
如果我将MMAdView作为Cordova WebView的兄弟视图插入(在顶部或底部停靠),则横幅广告始终显示全屏空白区域,其中包含WebView。
当我使用getWidth()
或getMeasuredWidth()
编写日志来检查MMAdView的大小时,它总是返回0或1而不是实际大小!
这与这个问题非常相似,区别在于我使用的是纯Java代码而不是XML布局文件。
AdMob mediated Millenial Media ad taking entire screen on refresh
见截图:
以下是代码:
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);
}
答案 0 :(得分:1)
经过研究,我自己找到了解决方案。
首先,创建横幅视图并设置逻辑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);
横幅视图无法返回正确的宽度和高度,必须从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来设置横幅视图的位置。
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);
现在可行,请参见截图。
答案 1 :(得分:0)
很难说出代码。这可能是因为您使用PhoneGap来包装一些广告代码。它可能是一种不同类型的错误。由于您已经在使用PhoneGap,因此您可能会发现Millennial Media的Javascript SDK更容易用于您的广告需求。 http://docs.millennialmedia.com/mmadlib/index.html