首页 > react-native开发android应用时,如何用WebView显示本地的文件

react-native开发android应用时,如何用WebView显示本地的文件

代码大概是这个样子,问题就在于不知道应该把这些资源文件放哪里。在iOS上我参考这篇文章解决了http://wl3.me/2015/05/01/react-in-your-react-native-webview-so-you-can-react-while-you-react.html。但在android上,不论我放在根目录,还是放在assets目录里,都无法正常载入。

const VIEW_URL = 'res/index.html';

class Demo extends Component {
  render() {
    return (
      <WebView
        ref="webview"
        style={styles.webview}
        source={{url: VIEW_URL}}
        javaScriptEnabled={true}
        domStorageEnabled={true}
        startInLoadingState={true}
        scalesPageToFit={true}
      />
    );
  }
}

关键是你的url的地址不对吧,asset的话根地址应该是这个吧file:///android_asset/
如果是下面的文件夹下的文件就应该是file:///android_asset/xx/xx.html


<WebView automaticallyAdjustContentInsets={false}
                    style={styles.webView}
                    source={require('./A.html')}    //载入指定文件,注意文件名的大小写
                    javaScriptEnabled={true}
                    domStorageEnabled={true}
                    startInLoadingState={true}/>
【热门文章】
【热门文章】