我在我的应用程序中使用Admob做广告,我遇到了一个问题。每当我将屏幕转到横向模式时,广告就会出现,但它的大小与纵向模式相同。这个问题是在我将清单中的xml声明添加到我的主活动中后发生的,这是保持应用程序的主要部分顺利运行所必需的:
android:configChanges="orientation|keyboardHidden|screenSize"我在我的广告中使用了智能横幅的尺寸:
ads:adSize="SMART_BANNER"我附上了这个问题的图片:


我必须做什么才能让广告在不删除的情况下在横向模式下正确调整大小
android:configChanges="orientation|keyboardHidden|screenSize" 在我的主要活动清单中吗?
发布于 2014-05-22 14:54:56
我是这样解决这个问题的:
@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());
}问候
发布于 2015-04-08 21:28:16
与其他响应一致(但更新为当前AdMob SDK -v7.5-,并提供完整代码),activity的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();https://stackoverflow.com/questions/20929963
复制相似问题