Google Ads MediaView not correctly resizing height to wrap_content when displaying image

后端 未结 5 433
情歌与酒
情歌与酒 2020-12-31 04:54

I got an email from AdMob today saying:

Change to native ads policy: Native ads will require MediaView to render the video or main image asset. In a

相关标签:
5条回答
  • 2020-12-31 05:05

    I was facing the same problem, after trying a lot with trial and error, I found that wrapping <FrameLayout...>(in which you are adding UnifiedNativeAdView) with ScrollView will resolve this problem.

    0 讨论(0)
  • 2020-12-31 05:09
    for (int i = 0; i < mediaView.getChildCount(); i++) {
        View view = mediaView.getChildAt(i);
        if (view instanceof ImageView) {
            ((ImageView) view).setAdjustViewBounds(true);
        }
    }
    

    This works for me.I tried Richard's answer,But it didn't works well in a RecyclerView.

    0 讨论(0)
  • 2020-12-31 05:13
    mediaView.setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() {
        @Override
        public void onChildViewAdded(View parent, View child) {
            if (child instanceof ImageView) {
                ImageView imageView = (ImageView) child;
                imageView.setAdjustViewBounds(true);
            }
        }
    
        @Override
        public void onChildViewRemoved(View parent, View child) {}
    });
    
    0 讨论(0)
  • 2020-12-31 05:20

    To ensure that all media are as wide as possible and that they respect a maximum height (so there are no surprises depending on the dimensions of the media).

    XML

    <com.google.android.gms.ads.formats.MediaView
         android:id="@+id/ad_media"
         android:layout_gravity="center_horizontal"
         android:layout_width="match_parent"
         android:layout_height="wrap_content" />
    

    JAVA

    mediaView.setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() {
            @Override
            public void onChildViewAdded(View parent, View child) {
                float scale = context.getResources().getDisplayMetrics().density;
                
                int maxHeightPixels = 175;
                int maxHeightDp = (int) (maxHeightPixels * scale + 0.5f);
    
                if (child instanceof ImageView) { //Images
                    ImageView imageView = (ImageView) child;
                    imageView.setAdjustViewBounds(true);
                    imageView.setMaxHeight(maxHeightDp);
    
                } else { //Videos
                    ViewGroup.LayoutParams params = child.getLayoutParams();
                    params.height = maxHeightDp;
                    child.setLayoutParams(params);
                }
            }
    
            @Override
            public void onChildViewRemoved(View parent, View child) {}
        });
    
    0 讨论(0)
  • 2020-12-31 05:24

    If implementing Advanced Native Ads, use "imageScaleType" property of MediaView as suggested here in the official docs https://developers.google.com/admob/android/native/advanced#setting_imagescaletype

    adView.mediaView.setImageScaleType(ImageView.ScaleType.CENTER_CROP)

    or any other ScaleType as per requirement.

    0 讨论(0)
提交回复
热议问题