项目中遇到WebView
/** * Created by tulin1 on 2017/2/14. */ import React, {Component} from 'react'; import { WebView, View, StyleSheet, Dimensions } from 'react-native'; import NavBar from '../Navigator/PublicNavBar' const {width, height} = Dimensions.get('window'); export default class WebViewCPIC extends Component { constructor(props){ super(props); this.state = { navTitle :"", scalesPageToFit:true } } render() { return ( <View style={{flex: 1,backgroundColor:'white'}}> <NavBar title={this.state.navTitle} backPage={()=>this.backPage()}/> <WebView style={styles.webview_style} ref="webViewRef" source={{uri: this.props.url}} javaScriptEnabled={true} domStorageEnabled={true} startInLoadingState={true} scalesPageToFit={true} onNavigationStateChange = {this._onNavigationStateChange.bind(this)} /> </View> ); } //获取页面title,URL,loading, canGoBack, canGoForward _onNavigationStateChange(navState){ let title = Math.abs(navState.title.indexOf('http'))?navState.title:''; this.setState({ navTitle:title, scalesPageToFit:true }); } backPage() { this.props.navigator.pop(); } } const styles = StyleSheet.create({ webview_style:{ backgroundColor:'white', }, });【好消息】个人网站已经上线运行,后面博客以及技术干货等精彩文章会同步更新,请大家关注收藏:http://www.lcode.org
今天我们一起来看一下WebView组件讲解以及使用实例
刚创建的React Native技术交流3群(496508742),React Native技术交流4群(458982758),请不要重复加群!,欢迎各位大牛,ReactNative技术爱好者加入交流!同时博客左侧欢迎微信扫描关注订阅号,移动技术干货,精彩文章技术推送!
该WebView组件进行创建渲染一个原生的WebView,进行加载一个网页。
上面我已经对于WebView组件的基本介绍以及相关属性方法做了讲解,下面我们用几个实例来演示一下WebView组件的使用。
3.1.先演示一个WebView组件最基本的使用方法,直接加载一个网页,具体代码如下:
[html] view plain copy 'use strict'; import React, { AppRegistry, Component, StyleSheet, Text, View, WebView, } from'react-native'; var DEFAULT_URL = 'http://www.lcode.org'; var WebViewDemo =React.createClass({ render: function() { return ( <View style={{flex:1}}> <TextstyleTextstyle={{height:40}}>简单的网页显示</Text> <WebViewstyleWebViewstyle={styles.webview_style} url={DEFAULT_URL} startInLoadingState={true} domStorageEnabled={true} javaScriptEnabled={true} > </WebView> </View> ); }, }); var styles =StyleSheet.create({ webview_style:{ backgroundColor:'#00ff00', } }); AppRegistry.registerComponent('WebViewDemo',() => WebViewDemo);
运行效果截图如下:
3.2.WebView加载本地的HTML静态字符串,具体代码如下:
[html] view plain copy 'use strict'; import React, { AppRegistry, Component, StyleSheet, Text, View, WebView, } from'react-native'; var DEFAULT_URL = 'http://www.lcode.org'; const HTML = ` <!DOCTYPEhtml>\n <html> <head> <title>HTML字符串</title> <metahttp-equivmetahttp-equiv="content-type" content="text/html;charset=utf-8"> <meta name="viewport"content="width=320, user-scalable=no"> <style type="text/css"> body { margin: 0; padding: 0; font: 62.5% arial, sans-serif; background: #ccc; } h1 { padding: 45px; margin: 0; text-align: center; color: #33f; } </style> </head> <body> <h1>加载静态的HTML文本信息</h1> </body> </html> `; var WebViewDemo =React.createClass({ render: function() { return ( <View style={{flex:1}}> <WebViewstyleWebViewstyle={styles.webview_style} html={HTML} startInLoadingState={true} domStorageEnabled={true} javaScriptEnabled={true} > </WebView> </View> ); }, }); var styles =StyleSheet.create({ webview_style:{ backgroundColor:'#00ff00', } }); AppRegistry.registerComponent('WebViewDemo',() => WebViewDemo);
运行效果截图如下: