20

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

更改原生广告政策:原生广告将要求 MediaView 呈现视频或主图像资源。为了帮助您更轻松地提供更好的广告体验,从 10 月 29 日开始,原生广告将需要 MediaView 来呈现视频或主图像资源。在此日期之前不合规的广告单元将停止投放广告,这可能会影响您的广告收入。

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

在Google 的这个codelab 示例中,使用了固定的高度和宽度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"/>

这就是我目前从上面的代码中得到的

这就是我需要的,并期望它看起来像使用wrap_content

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

有没有人有解决方法?如何在不对MediaView' 高度进行硬编码的情况下遵循新的 Google 要求?

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

4

5 回答 5

32
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) {}
});
于 2018-08-31T03:14:13.923 回答
2

我遇到了同样的问题,在尝试了很多尝试和错误之后,我发现包装<FrameLayout...>(你正在添加的地方UnifiedNativeAdViewScrollView可以解决这个问题。

于 2019-10-05T14:19:16.250 回答
1

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

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

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

于 2020-09-08T20:07:34.170 回答
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) {}
    });
于 2020-10-13T02:51:41.567 回答
0
for (int i = 0; i < mediaView.getChildCount(); i++) {
    View view = mediaView.getChildAt(i);
    if (view instanceof ImageView) {
        ((ImageView) view).setAdjustViewBounds(true);
    }
}

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

于 2020-01-08T11:29:39.487 回答