【问题标题】:Google Ads MediaView not correctly resizing height to wrap_content when displaying image显示图像时,Google Ads MediaView 未正确将高度调整为 wrap_content
【发布时间】:2019-02-05 11:04:06
【问题描述】:

我今天收到了一封来自 AdMob 的电子邮件:

更改原生广告政策:原生广告将要求 MediaView 渲染视频或主图像资产。努力帮助您交付 更轻松更好的广告体验,从 10 月 29 日开始,原生广告 将需要 MediaView 来呈现视频或主图像资产。广告 在此日期之前不合规的单位将停止投放广告,这可能 影响您的广告收入。

我在我的 Android 应用程序中尝试了这一点,删除了使用 ImageView 的图像和使用 MediaView 的视频的单独处理,但我发现 MediaView 没有根据图像的高度调整视图的高度显示。

在来自 Google 的 this 代码实验室示例中,使用了 MediaView 的固定高度和宽度。我不能这样做,因为这个屏幕会响应屏幕尺寸,这将根据设备而变化。可以动态调整图像大小的事实是使用UnifiedNativeAds 而不是预定义的广告(例如横幅)的主要好处之一。

这就是我需要显示MediaView 的方式,使用match_parent 表示宽度,使用wrap_content 表示高度。

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

This is what I am currently getting from the above code

This is what I need and expect it to look like from using wrap_content

在之前我们能够使用 ImageView 单独渲染图像的情况下,wrap_content 值正确地调整了图像的大小。

有没有人可以解决这个问题?如何在不对MediaView 的高度进行硬编码的情况下遵循新的 Google 要求?

我的完整代码可以在我在 github 上的演示应用程序中找到 here

【问题讨论】:

    标签: android admob google-dfp native-ads mediaview


    【解决方案1】:

    确保所有媒体都尽可能宽,并遵守最大高度(因此不会因媒体的尺寸而感到意外)。

    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) {}
        });
    

    【讨论】:

      【解决方案2】:

      如果实施高级原生广告,请按照官方文档https://developers.google.com/admob/android/native/advanced#setting_imagescaletype 中的建议使用 MediaView 的“imageScaleType”属性@

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

      或根据要求的任何其他 ScaleType。

      【讨论】:

        【解决方案3】:
        for (int i = 0; i < mediaView.getChildCount(); i++) {
            View view = mediaView.getChildAt(i);
            if (view instanceof ImageView) {
                ((ImageView) view).setAdjustViewBounds(true);
            }
        }
        

        这对我有用。我尝试了 Richard 的回答,但在 RecyclerView 中效果不佳。

        【讨论】:

          【解决方案4】:

          我也遇到了同样的问题,经过反复试验,我发现用ScrollView包裹&lt;FrameLayout...&gt;(在其中添加UnifiedNativeAdView)可以解决这个问题。

          【讨论】:

            【解决方案5】:
            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) {}
            });
            

            【讨论】:

            • 你能补充一下你改变了什么/为什么/有什么不同吗?
            • 谢谢!这正是我所需要的。
            • 请记住,如果它添加了视频,请不要担心。因为视频是 WebView,它适合布局没有问题。
            • @Richard 或任何其他根据support.google.com/admob/answer/6329638 知道的人(只有广告标题、URL、CTA 和图片资源可以点击(不可点击的“空白”空间)),白色区域不应可点击。如果我们匹配父级并且广告是纵向的,那么它会留下一些可点击的空白。是否有任何解决方法或不违反政策?
            • @Sarah 或根据 support.google.com/admob/answer/6329638 知道的任何其他人(只有广告标题、URL、CTA 和图片资源是可点击的(没有可点击的“空白”空间) )),白色区域不应该是可点击的。如果我们匹配父级并且广告是纵向的,那么它会留下一些可点击的空白。是否有任何解决方法或不违反政策?
            猜你喜欢
            • 2013-11-09
            • 1970-01-01
            • 1970-01-01
            • 2019-09-02
            • 1970-01-01
            • 1970-01-01
            • 2014-10-31
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多