使Admob Native Express广告充满活力 [英] Making Admob Native express ad dynamic
问题描述
我正在将Admob Native特快广告集成到我的应用程序中。我希望它能在所有大小的设备中显示。在小型设备中,由于广告尺寸而无法显示。我的添加是在listview和gridView中。
请建议我,它将如何加载到每种尺寸的设备中。甚至我都尝试动态设置大小,但对我不起作用。
I am integrating Admob Native express ad in my application. I want it to show in all size of devices. In small devices it is not showing because of ad size. My add is in listview and gridView. Please suggest me, how it will load add in each size of device. Even I was trying to set size dynamically, but did not Work for me.
预先感谢。
推荐答案
我成功使用了以下代码:
I had success with the following code:
private LinearLayout mAdContainer;
private NativeExpressAdView mAdmobAdView;
private AdRequest mAdmobAdRequest;
[...]
private void loadAds() {
// find out the width of the device in dp
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
float deviceWidthInDp = displayMetrics.widthPixels / displayMetrics.density;
int adWidth = (int)(deviceWidthInDp);
if (mAdmobAdView != null) {
// prevents a crash on older devices
mAdmobAdView.clearAnimation();
}
mAdContainer.removeAllViews();
mAdmobAdView = new NativeExpressAdView(this);
mAdmobAdView.setAdSize(new AdSize(adWidth, 250)); // AdSize(width, height), height ranges from 80 to 1200
mAdContainer.addView(mAdmobAdView);
mAdmobAdView.setAdUnitId(getString(R.string.admob_id)); // set your admob id
mAdmobAdRequest = new AdRequest.Builder()
.build();
mAdmobAdView.loadAd(mAdmobAdRequest);
}
编辑:感谢服装,指出在删除视图之前要清除动画,以防止旧设备崩溃(请参见此处)
thanks to fattire for pointing out to clear the animation before removing the view to prevent a crash on older devices (see here)
这篇关于使Admob Native Express广告充满活力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!