如何使用React Native内联渲染图像和文本?

时间:2018-03-24 12:13:01

标签: javascript react-native

如何在与文本相同的行上获取共享图标?

enter image description here

import React, {Component} from 'react';
import {
    ActivityIndicator,
    AsyncStorage,
    Dimensions,
    Image,
    ScrollView,
    Share,
    StatusBar,
    StyleSheet,
    TouchableOpacity,
    View
} from 'react-native';

import {Text} from 'react-native-elements';
import {Button} from 'react-native-share';
import Hyperlink from 'react-native-hyperlink'
import Icon from 'react-native-vector-icons/dist/FontAwesome';

const SCREEN_WIDTH = Dimensions.get('window').width;
const SCREEN_HEIGHT = Dimensions.get('window').height;

const IMAGE_SIZE = SCREEN_WIDTH - 80;

class CustomButton extends Component {
    constructor() {
        super();
        this.state = {
            selected: false
        };
    }

    componentDidMount() {
        const {selected} = this.props;
        this.setState({
            selected
        });
    }

    render() {
        const {title} = this.props;
        const {selected} = this.state;


        return (
            <Button
                title={title}
                titleStyle={{fontSize: 15, color: 'white'}}
                buttonStyle={selected ? {
                    backgroundColor: 'rgba(213, 100, 140, 1)',
                    borderRadius: 100,
                    width: 127
                } : {
                    borderWidth: 1,
                    borderColor: 'white',
                    borderRadius: 30,
                    width: 127,
                    backgroundColor: 'transparent'
                }}
                containerStyle={{marginRight: 10}}
                onPress={() => this.setState({selected: !selected})}
            />
        );
    }
}

class Fonts extends Component {
    constructor(props) {
        super(props);
        this.state = {
            ...this.state,
            selectedIndex: 0,
            value: 0.5,
            dataSource: null,
            isLoading: true,
            visible: false
        };
        this.componentDidMount = this.componentDidMount.bind(this);
    }

    getNavigationParams() {
        return this.props.navigation.state.params || {}
    }

    componentDidMount() {
        if (AsyncStorage.getItem('name')) {
            this.setState({'name': AsyncStorage.getItem('name')});
        }
        return fetch('http://www.koolbusiness.com/newvi/' + this.props.navigation.state.params.id + '.json')
            .then((response) => response.json())
            .then((responseJson) => {
                this.setState({
                    ...this.state,
                    isLoading: false,
                    dataSource: responseJson,
                    fontLoaded: true
                }, function () {
                });
            })
            .catch((error) => {
                console.error(error);
            });
    }

    onCancel() {
        console.log("CANCEL")
        this.setState({visible: false});
    }

    onOpen() {
        console.log("OPEN")
        this.setState({visible: true});
    }

    render() {
        if (this.state.isLoading) {
            return (
                <View style={{flex: 1, padding: 20}}>
                    <ActivityIndicator/>
                </View>
            )
        }

        let shareOptions = {
            message: 'http://www.koolbusiness.com/newvi/' + this.props.navigation.state.params.id + '.html',
            url: 'http://bam.tech',
            title: this.state.dataSource.title,
                subject: this.state.dataSource.title //  for email
        };

        return (
            <View style={{flex: 1}}>
                <StatusBar
                    barStyle="light-content"
                />

                {this.state.fontLoaded ?

                    <View style={{flex: 1, backgroundColor: 'rgba(47,44,60,1)'}}>

                        <View style={styles.statusBar}/>
                        <View style={styles.navBar}><TouchableOpacity onPress={() => {
                            Share.share(shareOptions);
                        }}>
                            <View>
                                <Text>     <Icon color="white" name="share-alt" size={42}/></Text>
                            </View>
                        </TouchableOpacity>
                            <Text style={styles.nameHeader}>
                                {this.state.dataSource.title}
                            </Text>
                        </View>
                        <ScrollView style={{flex: 1}}>
                            <View style={{justifyContent: 'center', alignItems: 'center'}}>
                                <Image
                                    source={{uri: this.state.dataSource.img ? this.state.dataSource.img : "http://www.koolbusiness.com/_/images/icons/electronics_icon.png"}}
                                    style={{width: IMAGE_SIZE, height: IMAGE_SIZE, borderRadius: 10}}
                                />

                            </View>
                            <View style={{
                                flex: 1,
                                flexDirection: 'row',
                                marginTop: 20,
                                marginHorizontal: 40,
                                justifyContent: 'center',
                                alignItems: 'center'
                            }}>
                                <Text style={{flex: 1, fontSize: 26, color: 'white'}}>
                                    {this.state.dataSource.title}
                                </Text>

                            </View>
                            <View style={{flex: 1, marginTop: 20, width: SCREEN_WIDTH - 80, marginLeft: 40}}>

                                <Text style={{flex: 1, fontSize: 15, color: 'white'}}>
                                    {this.state.dataSource.date}
                                </Text>
                                <Hyperlink linkDefault={true}>
                                <Text  style={{flex: 1, fontSize: 15, color: 'white'}} >
                                    {this.state.dataSource.text}
                                </Text>
                                </Hyperlink>
                            </View>


                        </ScrollView>
                    </View> :
                    <Text>Loading...</Text>
                }
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
    },    nameHeader: {
        color: 'white',
        fontSize: 22,
        textAlign: 'center'
    },
    textStyle: {
        fontSize: 18,
        padding: 10,
    },
});

export default Fonts;

它按预期工作,但我想在与标题文本相同的行上呈现共享图标。 可以吗?

1 个答案:

答案 0 :(得分:3)

您可以使用&#34; flexDirection&#34;在视图中添加&#39;&#39; 和组共享图标以及标题:

<View style={styles.navBar}>
   <TouchableOpacity onPress={() => {Share.share(shareOptions); }}>
          <View>
              <Text>     
                 <Icon color="white" name="share-alt" size={42}/>
              </Text>
           </View>
         </TouchableOpacity>
         <Text style={styles.nameHeader}>
               {this.state.dataSource.title}
         </Text>
       </View>

将以下样式添加到navBar className:

navBar: {
    flex: 1,
    flexDirection: 'row'
  },

此外,您在TouchableOpacity中使用了Text和View,我认为这是不需要的,所以我删除了那些并在展会上做了类似于您的代码的小吃,请在此处查看:

https://snack.expo.io/Hk3lrAQ9f

相关问题