简体   繁体   English

在Android上的本机React内部使用Google IMA Webview时未显示

[英]Google IMA webview is not showing when used inside react native on android

In short: We use reacte native on android and use the native VideoView to show videos with ads using the Google IMA SDK for android. 简而言之:我们使用Android上的reacte本机,并使用本机VideoView通过适用于Android的Google IMA SDK来显示带有广告的视频。 The video plays, the ads play, but the Webview containing "read more" and "skip ad" is not visible. 视频播放,广告播放,但是包含“阅读更多”和“跳过广告”的Web视图不可见。 It's added to the view hierarchy, but it's size 0,0 and it has no children. 它已添加到视图层次结构中,但是大小为0,0,并且没有子级。 When I use the exact same code in a native app. 当我在本机应用程序中使用完全相同的代码时。 Everything works as expected. 一切正常。

Some more context: 更多背景信息:

  • The video player / google ima sdk integration is copied straight from the google ima example app. 视频播放器/ google ima sdk集成是直接从google ima示例应用中复制的。
  • When the component is placed in a native anroid app everything works fine. 当组件放置在本地anroid应用程序中时,一切正常。
  • When inspecting the view-hierachy it's clear the webview is added and it's size is 0,0 在检查视图层次结构时,很明显已添加了webview,其大小为0,0
  • When adding a native webview myself it's is rended perfectly fine. 当我自己添加本机Webview时,可以很好地使用它。

Relevant code from the React Native component: 来自React Native组件的相关代码:

@SuppressLint("ViewConstructor")
public class ReactVideoViewWithAds extends FrameLayout implements
    LifecycleEventListener {

private VideoPlayerWithAdPlayback mVideoPlayerWithAdPlayback;
private RelativeLayout mCompanionAdSlot;
private VideoPlayerController mVideoPlayerController;

public ReactVideoViewWithAds(ThemedReactContext themedReactContext) {
    super(themedReactContext);
    createViews();
    themedReactContext.addLifecycleEventListener(this);
}

private void createViews() {

    FrameLayout.LayoutParams companionAdLayoutParams = new FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            (int) (50 * getResources().getSystem().getDisplayMetrics().density));
    companionAdLayoutParams.gravity = Gravity.CENTER_HORIZONTAL;
    mCompanionAdSlot = new RelativeLayout(getContext());
    mCompanionAdSlot.setBackgroundColor(Color.BLUE);
    addView(mCompanionAdSlot, companionAdLayoutParams);

    //player with ad playback
    FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.MATCH_PARENT);
    mVideoPlayerWithAdPlayback = new VideoPlayerWithAdPlayback(getContext());
    addView(mVideoPlayerWithAdPlayback, layoutParams);


    initPlayer();
}

private void initPlayer() {
    mVideoPlayerController = new VideoPlayerController(
            getContext(),
            mVideoPlayerWithAdPlayback,
            null,
            null,
            "nl",
            mCompanionAdSlot,
            null);
}

This question had the solution in a comment 这个问题在评论中有解决方案

When adding views dynamically in a native component it seems you need to manually trigger a layout cycle in order for react-native to pick up on the changes. 在本机组件中动态添加视图时,您似乎需要手动触发布局周期,以便react-native能够获取更改。 (perhaps related to react native updating it's shadow-view) (可能与本机更新的影子视图有关)

 @Override
    public void requestLayout() {
        super.requestLayout();
        post(measureAndLayout);
    }

    private final Runnable measureAndLayout = new Runnable() {
        @Override
        public void run() {
            measure(
                    MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY),
                    MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.EXACTLY));
            layout(getLeft(), getTop(), getRight(), getBottom());
        }
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM