问题描述
我今天收到了一封来自 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 的 codelab 示例,使用了 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.
这就是我需要显示 MediaView
的方式,使用 match_parent
作为宽度,使用 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!