Android 上的 Admob:加载前未保留横幅 space

Admob on Android: banner space not reserved before loading

我们在 Android 应用程序上使用 AdMob 已超过 4 年。 在过去的几天里,我们在没有修改任何代码的情况下遇到了 AdMob 的问题。

如下图所示:

===

下面是我们的实现描述:

我们将横幅广告放置在片段屏幕顶部约 20% 的位置,在 LinearLayout "banner_container"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
....
<LinearLayout android:id="@+id/banner_container"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
....
</LinearLayout>

在 Fragment 的 "onCreateView" 上,我们将横幅添加到容器中

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

...

adView = new AdView(getActivity()); 
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId(AD_UNIT_ID);

LinearLayout mBannerContainer = rootView.findViewById(R.id.banner_container); 
mBannerContainer.setVisibility(View.VISIBLE); 
mBannerContainer.addView(adView);

AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .build();

adView.loadAd(adRequest);

...

}

===

我们如何恢复到加载时横幅 space 已被保留的情况?

由于 Admob 在加载之前不保留横幅高度,因此解决方案似乎是手动完成。

根据 Admob guide for Banner Ads:

Smart Banners are ad units that will render screen-wide banner ads on any screen size across different devices in either orientation. Smart Banners help deal with increasing screen fragmentation across different devices by "smartly" detecting the width of the phone in its current orientation, and making the ad view that size.

Three ad heights (in dp, density-independent pixel) are available:

32 - used when the screen height of a device is less than 400 50 - used when the screen height of a device is between 400 and 720 90 - used when the screen height of a device is greater than 720

解决方案 1

您使用以下方法知道身高:

    public static int getAdViewHeightInDP(Activity activity) {
         int adHeight = 0;

         int screenHeightInDP = getScreenHeightInDP(activity);
         if (screenHeightInDP < 400)
             adHeight = 32;
         else if (screenHeightInDP >= 400 && screenHeightInDP <= 720)
             adHeight = 50;
         else
             adHeight = 90;

         return adHeight;
     }
    

     public static int getScreenHeightInDP(Activity activity) {
         DisplayMetrics displayMetrics = ((Context) activity).getResources().getDisplayMetrics();
    
         float screenHeightInDP = displayMetrics.heightPixels / displayMetrics.density;
    
         return Math.round(screenHeightInDP);
     }

在您的“banner_container”中删除:

android:visibility="gone"

更改片段“onCreateView”:

    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

         ...

         adView = new AdView(getActivity()); 
         adView.setAdSize(AdSize.SMART_BANNER);
         adView.setAdUnitId(AD_UNIT_ID);

         LinearLayout mBannerContainer = rootView.findViewById(R.id.banner_container); 
         mBannerContainer.setLayoutParams(
              new LinearLayout.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT,
                            getAdViewHeightInDP(this.getActivity())
               ));
         )
         mBannerContainer.addView(adView);

         AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .build();

         adView.loadAd(adRequest);

         ...

     }

解决方案 2

不使用方法“getAdViewHeightInDP”和“getScreenHeightInDP”,而是使用方法“AdSize.SMART_BANNER.getHeightInPixels (this)”。

在您的“banner_container”中删除:

android:visibility="gone"

更改片段“onCreateView”:

    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

         ...

         adView = new AdView(getActivity()); 
         adView.setAdSize(AdSize.SMART_BANNER);
         adView.setAdUnitId(AD_UNIT_ID);

         LinearLayout mBannerContainer = rootView.findViewById(R.id.banner_container); 
         mBannerContainer.setLayoutParams(
              new LinearLayout.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT,
                            AdSize.SMART_BANNER.getHeightInPixels(this.getActivity())
               ));
         )
         mBannerContainer.addView(adView);

         AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .build();

         adView.loadAd(adRequest);

         ...

     }

只是从评论中重申并用代码块澄清,最简单的做法是:

LinearLayout layout = (LinearLayout) findViewById(R.id.mainLayout); layout.addView(adView, 0); adView.getLayoutParams().height = AdSize.SMART_BANNER.getHeightInPixels(this);

我真的希望 Admob 了解广告加载行为的这一变化,以便开发人员了解并避免意外点击。