Admob 广告在屏幕方向上未正确调整大小 [包括图片]

2022-01-25 00:00:00 android java admob

我在我的应用中使用 Admob 投放广告,但遇到了问题.每当我将屏幕切换到横向模式时,广告就会出现,但它的大小与纵向模式下的大小相同.在我将清单中的这个 xml 声明添加到我的主要活动中后发生了这个问题,这是保持应用程序主要部分顺利运行所必需的:

I am using Admob for ads in my app and I ran into a problem. Whenever I turn the screen to landscape mode, the ad shows up but it's the same size as it was in portrait mode. This problem occured after I added this xml declaration in my manifest to my main activity that was necessary to keep the main parts of the app functioning smoothly:

android:configChanges="orientation|keyboardHidden|screenSize"

我在我的广告中使用智能横幅的尺寸:

I am using smart banner in my ad for the size:

ads:adSize="SMART_BANNER"

我已经附上了这个问题的图片:

I have attached pictures of this problem:

我必须做些什么才能让广告在横向模式下正确调整大小而不删除

What do I have to do to get the ad to resize properly in landscape mode without deleting

android:configChanges="orientation|keyboardHidden|screenSize"  

在我的主要活动的清单中?

in the manifest for my main activity?

推荐答案

与其他响应一致(但更新到当前的 AdMob SDK -v7.5-,并提供完整代码),onConfigurationChanged() 方法该活动需要包括广告视图的销毁和创建:

In line with other responses (but updating them to the current AdMob SDK -v7.5-, and providing the full code), the onConfigurationChanged() method of the activity needs to include the destruction and creation of the Ad View:

// 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();

相关文章