gpt4 book ai didi

android - Android 上的 Admob : banner space not reserved before loading

转载 作者:太空宇宙 更新时间:2023-11-03 12:02:39 24 4
gpt4 key购买 nike

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

如下图所示:

  • 以前,在加载横幅之前保留横幅空间
  • 现在,横幅空间在加载前没有预留,这给用户带来了非常烦人的体验,他们会看到横幅加载后内容向下移动

enter image description here

===

下面是我们实现的描述:

我们将横幅广告放置在 fragment 屏幕顶部约 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);

...

}

===

我们如何恢复到加载时横幅空间已被预留的情况?

最佳答案

由于 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 40050 - used when the screen height of a device is between 400 and 72090 - used when the screen height of a device is greater than 720

解决方案一

您可以使用以下方法知道高度:

    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"

更改 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.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);

...

}

方案二

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

在您的“banner_container”中删除:

android:visibility="gone"

更改 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.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);

...

}

关于android - Android 上的 Admob : banner space not reserved before loading,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53166028/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com