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

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

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

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rootView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    ...
<FrameLayout
        android:id="@+id/frame_layout_admob_banner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:maxHeight="110dp"
        android:minHeight="32dp"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
void setupAdViewIfNotPaid() {
    if (...) {
         return;
    }
    ConstraintLayout root = findViewById(R.id.rootView);
    int screenHeightInDp = getScreenHeightInDP(this);
    int bannerHeightInDp = getMaxAdViewHeightInDP(screenHeightInDp);
    root.findViewById(R.id.space_bottom).getLayoutParams().height = dpToPx(bannerHeightInDp);
    loadAdMobBannerAds(bannerHeightInDp);
}
private void loadAdMobBannerAds(int maxBannerHeightInDp) {
    ConstraintLayout root = findViewById(R.id.rootView);
    FrameLayout frameLayout = root.findViewById(R.id.frame_layout_admob_banner);
    frameLayout.setVisibility(View.VISIBLE);
    final String adUnitId = Tools.getMainBannerAdUnit(this);
    mAdView = new AdView(this);
    mAdView.setId(R.id.adView);
    mAdView.setAdUnitId(adUnitId);
    frameLayout.addView(mAdView);
    mAdView.setAdSize(getAdSize(maxBannerHeightInDp));
    mAdView.loadAd(Tools.getAdRequest());
}
public static int getScreenHeightInDP(Activity activity) {
    DisplayMetrics displayMetrics = ((Context) activity).getResources().getDisplayMetrics();
    float screenHeightInDP = displayMetrics.heightPixels / displayMetrics.density;
    return Math.round(screenHeightInDP);
}
public static int getMaxAdViewHeightInDP(int screenHeightInDp) {
    int adHeight = 0;
    if (screenHeightInDp < 400)
        adHeight = 32;
    else if (screenHeightInDp <= 720)
        adHeight = 50;
    else
        adHeight = 90;
    return adHeight;
}
private AdSize getAdSize(int maxBannerHeightInDp) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        int screenWidthDp = getResources().getConfiguration().screenWidthDp;
        return AdSize.getInlineAdaptiveBannerAdSize(screenWidthDp, maxBannerHeightInDp);
    } else {
        Display display = getWindowManager().getDefaultDisplay();
        DisplayMetrics outMetrics = new DisplayMetrics();
        display.getMetrics(outMetrics);
        float widthPixels = outMetrics.widthPixels;
        float density = outMetrics.density;
        int adWidthInDp = (int) (widthPixels / density);
        return AdSize.getInlineAdaptiveBannerAdSize(adWidthInDp, maxBannerHeightInDp);
    }
}

暂无答案!

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

相关问题