其他分享
首页 > 其他分享> > 在Android上渲染WebView时,屏幕闪烁一次

在Android上渲染WebView时,屏幕闪烁一次

作者:互联网

我使用react-native-webview在我的React Native应用中渲染一个WebView.
在iOS上一切正常,但是在Android上,渲染WebView时,我的屏幕闪烁(先黑后白,然后显示网页).

根据this answer,我尝试在< application>中添加android:hardwareAccelerated =“ false”.我的AndroidManifest.xml标签,但这并不能解决我的问题. (此外,它会隐藏使用高程样式属性创建的所有阴影效果)

<DelayedComponent key={key} style={{ height: 200, ...style }}>
  <WebView
    style={{ alignSelf: "stretch" }}
    source={{ uri: /* a youtube url */ }}
    renderLoading={() => <Loading />}
    startInLoadingState={true}
    scrollEnabled={false}
  />
</DelayedComponent>

< DelayedComponent>只是呈现< WebView>一秒钟后(使用基本setTimeout).

export class DelayedComponent extends React.PureComponent<
  { delay?: number; renderLoading?: any } & ViewProps,
  { show: boolean }
> {
  constructor(props) {
    super(props);
    this.state = { show: false };
  }

  public render() {
    console.log("RENDER DELAYED", this.state);
    const loadingComp = this.props.renderLoading || (
      <Text>Just wait a second...</Text>
    );
    const { delay, renderLoading, ...forwardProps } = this.props;
    return (
      <View {...forwardProps}>
        {this.state.show ? this.props.children : loadingComp}
      </View>
    );
  }

  public async componentDidMount() {
    const delay = this.props.delay || 1000;
    await (() => new Promise(resolve => setTimeout(resolve, delay)))();
    this.setState({ show: true });
  }
}

屏幕在< DelayedComponent>之后闪烁一秒钟.当< WebView>被陈列.

这里是一个视频链接,显示发生疯狂:https://drive.google.com/open?id=1dX7ofANFI9oR2DFCtCRgI3_0DcsOD12B

我希望呈现WebView时屏幕不会闪烁,就像在iOS设备上一样.

谢谢您的帮助 !

解决方法:

只需将WebView样式的不透明度设置为0.99.

< WebView样式= {{不透明度:0.99}} />

它可能与以下内容有关:
Rendering webview on android device overlaps previous siblings from same parent

编辑:将WebView组件包装在View中,并带有溢出:’hidden’:< View style = {{flex:1,溢出:'hidden'}}>< WebView ... />< / View>也可以,但是由于某种原因有时可能导致应用程序崩溃.

标签:react-native,android-webview,android
来源: https://codeday.me/bug/20191211/2106956.html