我在我的应用中使用Admob广告,但我遇到了问题。每当我将屏幕转为横向模式时,广告就会显示,但它与纵向模式下的尺寸相同。在我的清单中将此xml声明添加到我的主要活动之后出现此问题,这是保持应用程序主要部分顺利运行所必需的:
android:configChanges="orientation|keyboardHidden|screenSize"
我在广告中使用智能横幅广告:
ads:adSize="SMART_BANNER"
我附上了这个问题的照片:
如何在横向模式下正确调整广告大小而不删除
,我该怎么办?android:configChanges="orientation|keyboardHidden|screenSize"
在我的主要活动的清单中?
答案 0 :(得分:7)
这就是我解决它的方法:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
orientation_changed = true;
renewAd();
}
private void renewAd() {
AdView ad = (AdView) findViewById(R.id.adView);
LayoutParams lp = (LayoutParams) ad.getLayoutParams();
// change relative layout for your layout which contains the adView
RelativeLayout parent = (RelativeLayout) ad.getParent();
parent.removeView(ad);
AdView newAd = new AdView(this, AdSize.SMART_BANNER, "YOUR ID");
newAd.setId(R.id.adView);
newAd.setLayoutParams(lp);
parent.addView(newAd);
newAd.loadAd(new AdRequest());
}
问候
答案 1 :(得分:6)
与其他回复一致(但将其更新为当前的AdMob SDK -v7.5-并提供完整代码),活动的onConfigurationChanged()方法需要包含销毁和创建广告视图:
// Remove the ad keeping the attributes
AdView ad = (AdView) myactivity.findViewById(R.id.adView);
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) ad.getLayoutParams();
LinearLayout parentLayout = (LinearLayout) ad.getParent();
parentLayout.removeView(ad);
// Re-initialise the ad
ad.destroy();
ad = new AdView(parent);
ad.setAdSize(com.google.android.gms.ads.AdSize.SMART_BANNER);
ad.setAdUnitId(myactivity.getString(R.string.banner_ad_unit_id));
ad.setId(R.id.adView);
ad.setLayoutParams(lp);
parentLayout.addView(ad);
// Re-fetch add and check successful load
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice(parent.getString(R.string.test_device_id))
.build();