React native Webview的使用

使用WebView的方法:

1、申明WebView变量

2、调用方法有两种:

(1)使用接口对接、

(2)直接使用url方式把内容全部取下来

3、使用url呈现的方式如下

问题处理方法及结果:

1、使用如下官方案例代码:

[javascript]  view plain  copy
  1. 'use strict';  
  2. import React, {  
  3.   AppRegistry,  
  4.   Component,  
  5.   StyleSheet,  
  6.   Text,  
  7.   View,  
  8.   WebView,  
  9. } from 'react-native';  
  10. var html = '<!DOCTYPE html><html><body><h1>This is a heading!</body></html>';  
  11. var HelloWorld = React.createClass({  
  12.   render: function() {  
  13.     return (  
  14.       <View style={styles.container}>  
  15.         <WebView source={{html: html}}/>  
  16.       </View>  
  17.     );  
  18.   },  
  19. });  
  20. var styles = StyleSheet.create({  
  21.     container:{    
  22.        backgroundColor:'#00ff00',  
  23.        height:200,  
  24.        width:300,  
  25.        flex:1,  
  26.     }  
  27. });  
  28.    
  29. AppRegistry.registerComponent('HelloWorld', () => HelloWorld);  

如下是结果:

3、第三部、将官方案例代码放入到自己的代码中、查看可否运行

[javascript]  view plain  copy
  1. var React = require('react-native');  
  2. var Util = require('./../common/util');  
  3. var ServiceURL = require('./../common/service');  
  4. var PublicItem = require('./public_item');  
  5. var Header = require('./../common/header');  
  6. var HTMLView = require('react-native-htmlview')  
  7. var {  
  8.     StyleSheet,  
  9.     Text,  
  10.     View,  
  11.     ListView,  
  12.     Image,  
  13.     ScrollView,  
  14.     TouchableOpacity,  
  15.     WebView  
  16. } = React;  
  17. var html = '<!DOCTYPE html><html><body><h1>This is a heading!</body></html>';  
  18. module.exports = React.createClass({  
  19.     getInitialState: function() {  
  20.         return {  
  21.             data: null  
  22.         };  
  23.     },  
  24.     render: function() {  
  25.         return (  
  26.             // <ScrollView style={styles.m10}>  
  27.         // {  
  28.           this.state.data ?  
  29.             <View>  
  30.               <Header  
  31.                 navigator={this.props.navigator}  
  32.                 initObj={{  
  33.                     backName: '主页',  
  34.                     title: '舆情详情'  
  35.                 }}/>  
  36.               <PublicItem row={this.state.data}/>  
  37.               <View style={styles.container}>  
  38.                 <WebView  
  39.                    // source={{html: this.state.data.content}}  
  40.                     source={{html: html}}  
  41.                    // source={{url: 'http://120.26.216.13:6080/yql/ws/contentdetail?uuid=9678940aeb2203c8542bfa6de01863b2'}}  
  42.                 />  
  43.             </View>  
  44.               <View style={[styles.row,{marginTop:10}]}>  
  45.                 <Text style={[styles.title]}>来源:搜狗新闻</Text>  
  46.                 <Text style={[styles.pages]}>点击:27</Text>  
  47.                 <Text style={[styles.pages]}>转发:88</Text>  
  48.                 <Text style={[styles.pages]}>评论:4</Text>  
  49.                 <Text style={[styles.pages]}>this.state.content</Text>  
  50.               </View>  
  51.               <View>  
  52.                 <Text style={[styles.title]}>相似舆情</Text>  
  53.                 <Text style={[styles.text]}>江苏科技大学:青春青春青春青春大赛</Text>  
  54.                 <Text style={[styles.text]}>江苏科技大学:青春青春青春青春大赛</Text>  
  55.                 <Text style={[styles.text]}>江苏科技大学:青春青春青春青春大赛</Text>  
  56.                 <Text style={[styles.text]}>江苏科技大学:青春青春青春青春大赛</Text>  
  57.                 <Text style={[styles.text]}>江苏科技大学:青春青春青春青春大赛</Text>  
  58.               </View>  
  59.               <View style={{height:100}}></View>  
  60.             </View>  
  61.             : Util.loading  
  62.         // }  
  63.       // </ScrollView>  
  64.         );  
  65.     },  
  66.   
  67.     componentDidMount: function() {  
  68.         var uuid = this.props.id;  
  69.         var that = this;  
  70.         var url = ServiceURL.public_detail + '?uuid=' + uuid;  
  71.         Util.get(url, function(data) {  
  72.             that.setState({  
  73.                 data: data.detail  
  74.             });  
  75.         }, function(err) {  
  76.             alert(err);  
  77.         });  
  78.     }  
  79. });  
  80.   
  81. var styles = StyleSheet.create({  
  82.     container:{    
  83.        backgroundColor:'#00ff00',  
  84.        height:200,  
  85.        width:300,  
  86.        flex:1,  
  87.     },  
  88.     m10: {  
  89.         flex: 1  
  90.     },  
  91.     title: {  
  92.         fontSize: 16,  
  93.         marginLeft: 10,  
  94.         marginTop: 10,  
  95.         marginBottom: 10  
  96.     },  
  97.     text: {  
  98.         marginLeft: 10,  
  99.         marginRight: 10,  
  100.         color: '#666666'  
  101.     },  
  102.     webviewContent: {  
  103.         height:500  
  104.     },  
  105.     pages: {  
  106.         marginLeft: 30,  
  107.         color: '#A7A0A0'  
  108.     },  
  109.     row: {  
  110.         flexDirection: 'row'  
  111.     },  
  112. });  

样式如下:说明没有任何问题:

继续查原因、对比官方文档和自己代码的差异、发现官方代码中webview是嵌套在view中的、所以问题就出在我之前的代码webview在外层没有嵌套在view中

继续测试、把代码模块化、

[javascript]  view plain  copy
  1. <View style={styles.container}>  
  2.     <WebView  
  3.         source={{html: this.state.data.content}}  
  4.         // source={{html: html}}  
  5.         // source={{url: 'http://120.26.216.13:6080/yql/ws/contentdetail?uuid=9678940aeb2203c8542bfa6de01863b2'}}  
  6.     />  
  7.  </View>  

最重结果、nice

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值