admob自适应广告横幅占据了几乎一半的屏幕(纵向模式)

atmip9wb  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(288)

admob自适应广告横幅占据了几乎一半的屏幕(纵向模式)
起初,一切正常,但如果你等一等,广告就会更新,占用更多的空间(几乎是屏幕的一半)
我在华为y6安卓6上复制了它
也许有人碰到了这个?如何解决这个问题?我已经尝试了很多选择。

  1. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:app="http://schemas.android.com/apk/res-auto"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:id="@+id/rootView"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7. ...
  8. <FrameLayout
  9. android:id="@+id/frame_layout_admob_banner"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:background="@android:color/white"
  13. android:maxHeight="110dp"
  14. android:minHeight="32dp"
  15. android:visibility="invisible"
  16. app:layout_constraintBottom_toBottomOf="parent"
  17. app:layout_constraintEnd_toEndOf="parent"
  18. app:layout_constraintStart_toStartOf="parent" />
  19. </androidx.constraintlayout.widget.ConstraintLayout>
  1. void setupAdViewIfNotPaid() {
  2. if (...) {
  3. return;
  4. }
  5. ConstraintLayout root = findViewById(R.id.rootView);
  6. int screenHeightInDp = getScreenHeightInDP(this);
  7. int bannerHeightInDp = getMaxAdViewHeightInDP(screenHeightInDp);
  8. root.findViewById(R.id.space_bottom).getLayoutParams().height = dpToPx(bannerHeightInDp);
  9. loadAdMobBannerAds(bannerHeightInDp);
  10. }
  11. private void loadAdMobBannerAds(int maxBannerHeightInDp) {
  12. ConstraintLayout root = findViewById(R.id.rootView);
  13. FrameLayout frameLayout = root.findViewById(R.id.frame_layout_admob_banner);
  14. frameLayout.setVisibility(View.VISIBLE);
  15. final String adUnitId = Tools.getMainBannerAdUnit(this);
  16. mAdView = new AdView(this);
  17. mAdView.setId(R.id.adView);
  18. mAdView.setAdUnitId(adUnitId);
  19. frameLayout.addView(mAdView);
  20. mAdView.setAdSize(getAdSize(maxBannerHeightInDp));
  21. mAdView.loadAd(Tools.getAdRequest());
  22. }
  23. public static int getScreenHeightInDP(Activity activity) {
  24. DisplayMetrics displayMetrics = ((Context) activity).getResources().getDisplayMetrics();
  25. float screenHeightInDP = displayMetrics.heightPixels / displayMetrics.density;
  26. return Math.round(screenHeightInDP);
  27. }
  28. public static int getMaxAdViewHeightInDP(int screenHeightInDp) {
  29. int adHeight = 0;
  30. if (screenHeightInDp < 400)
  31. adHeight = 32;
  32. else if (screenHeightInDp <= 720)
  33. adHeight = 50;
  34. else
  35. adHeight = 90;
  36. return adHeight;
  37. }
  38. private AdSize getAdSize(int maxBannerHeightInDp) {
  39. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
  40. int screenWidthDp = getResources().getConfiguration().screenWidthDp;
  41. return AdSize.getInlineAdaptiveBannerAdSize(screenWidthDp, maxBannerHeightInDp);
  42. } else {
  43. Display display = getWindowManager().getDefaultDisplay();
  44. DisplayMetrics outMetrics = new DisplayMetrics();
  45. display.getMetrics(outMetrics);
  46. float widthPixels = outMetrics.widthPixels;
  47. float density = outMetrics.density;
  48. int adWidthInDp = (int) (widthPixels / density);
  49. return AdSize.getInlineAdaptiveBannerAdSize(adWidthInDp, maxBannerHeightInDp);
  50. }
  51. }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题