Google Ads MediaView在显示图像时未正确将高度调整为wrap_content [英] Google Ads MediaView not correctly resizing height to wrap_content when displaying image
问题描述
我今天收到了AdMob的电子邮件,
I got an email from AdMob today saying:
更改原生广告政策:原生广告将MediaView设置为
渲染视频或主图像资产。为了帮助您更轻松地交付
更好的广告体验,从10月29日开始,原生广告
将要求MediaView呈现视频或主图像资产。在该日期之前不符合要求的
个广告单元将停止投放广告,这可能会影响
的广告收入。
Change to native ads policy: Native ads will require MediaView to render the video or main image asset. In an effort to help you deliver a better ad experience more easily, beginning October 29th, native ads will require MediaView to render the video or main image asset. Ad units not compliant by this date will stop serving ads, which could impact your ad revenue.
我在Android应用程序中进行了尝试,使用 ImageView
删除了对图像的单独处理,而使用 MediaView
删除了对视频的单独处理,但是我发现MediaView并未根据其显示图像的高度调整视图高度的大小。
I tried this out in my Android app, removing the separate handling of images with ImageView
and video with MediaView
, but I have found that the MediaView is not resizing the view's height according to the height of the image it displays.
在此代码示例(来自Google),使用了 MediaView
的固定高度和宽度。我无法执行此操作,因为此屏幕响应屏幕尺寸,该尺寸将根据设备而变化。可以动态调整图像大小是使用 UnifiedNativeAds
代替预定义广告(例如横幅)的主要好处之一。
In this codelab example from Google, a fixed height and width for the MediaView
are used. I cannot do this, as this screen is responsive to the screen size, which will change depending on the device. The fact that the image can be dynamically resized is one of the main benefits for using UnifiedNativeAds
instead of predefined ads such as banners.
这就是我需要使用 match_parent $显示
MediaView
的方式c $ c>表示宽度, wrap_content
表示高度。
This is how I need to be displaying the MediaView
, using match_parent
for width and wrap_content
for height.
<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
值可以正确调整图像大小。
In the previous case where we were able to render the images separately using ImageView, the wrap_content
value correctly sized the image.
有人对此有解决方法吗?如何在不对 MediaView
的高度进行硬编码的情况下遵循Google的新要求?
Does anyone have a workaround for this? How can I follow the new Google requirements without hardcoding the MediaView
's height?
可以找到我的完整代码此处,位于我在github上的演示应用程序中。
My full code can be found here, in my demo app on github.
推荐答案
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) {}
});
这篇关于Google Ads MediaView在显示图像时未正确将高度调整为wrap_content的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!