React-Native学习笔记之:使用Tab react-native-tab-navigator框架

在React Native中,我们通常使用react-native-tab-navigator来做底部导航栏效果,使用前先要在项目中引入对应 依赖库,
引用方法:

windows环境,cmd命令窗口,进入到项目所在的文件目录,执行:

npm install react-native-tab-navigator --save

执行完成后,项目中即添加上了react-native-tab-navigator依赖库。使用如下:

import React, {Component} from 'react';

import {
    AppRegistry,
    StyleSheet,
    Text,
    View,
    Image
} from 'react-native';
//导入react-native-tab-navigator方式:
//cmd项目路径下执行npm install react-native-tab-navigator --save
import TabNavigator from 'react-native-tab-navigator'
export default class Tabnavigator extends Component {
    constructor(props) {
        super(props);
        this.state = {
            selectedTab: 'Event'
        }
    }

    render() {
        return (
            
                
                    //设置选中的位置
                        selected={this.state.selectedTab === 'Event'}
                        //标题
                        title="Event"
                        //标题样式
                        titleStyle={styles.tabText}
                        //选中时标题文字样式
                        selectedTitleStyle={styles.selectedTabText}
                        //图标
                        renderIcon={() => require("./res/images/icon_alarm_normal.png")} />}
                        //选中时图标
                        renderSelectedIcon={() => tintColor:'red'}]} source={require("./res/images/icon_alarm_normal.png")} />}
                        //点击Event
                        onPress={() => this.setState({ selectedTab: 'Event' })}>
                        
                            fontSize:18,padding:15,color: 'blue'}}>This is Event Page
                        
                    
                    this.state.selectedTab === 'Log'}
                        title="Log"
                        titleStyle={styles.tabText}
                        selectedTitleStyle={styles.selectedTabText}
                        renderIcon={() => require("./res/images/icon_event.png")} />}
                        renderSelectedIcon={() =>  tintColor:'red'}]} source={require("./res/images/icon_event.png")} />}
                        onPress={() => this.setState({ selectedTab: 'Log' })}>
                        
                            fontSize:18,padding:15,color: 'blue'}}>This is Log Page
                        
                    
                    this.state.selectedTab === 'Device'}
                        title="Device"
                        titleStyle={styles.tabText}
                        selectedTitleStyle={styles.selectedTabText}
                        renderIcon={() => require("./res/images/icon_device_normal.png")} />}
                        renderSelectedIcon={() => tintColor:'red'}]} source={require("./res/images/icon_device_normal.png")} />}
                        onPress={() => this.setState({ selectedTab: 'Device' })}>
                        
                            fontSize:18,padding:15,color: '#fff'}}>This is Device Page
                        
                    
                    this.state.selectedTab === 'User'}
                        title="User"
                        titleStyle={styles.tabText}
                        selectedTitleStyle={styles.selectedTabText}
                        renderIcon={() => require("./res/images/icon_user_normal.png")} />}
                        renderSelectedIcon={() => tintColor:'red'}]} source={require("./res/images/icon_user_normal.png")} />}
                        onPress={() => this.setState({ selectedTab: 'User' })}>
                        
                            fontSize:18,padding:15,color: '#fff'}}>This is User Page
                        
                    
                
            
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1
    },
    tabText: {
        fontSize: 10,
        color: 'black'
    },
    selectedTabText: {
        fontSize: 10,
        color: 'red'
    },
    icon: {
        width: 22,
        height: 22
    },
    page0: {
        flex: 1,
        backgroundColor: 'yellow'
    },
    page1: {
        flex: 1,
        backgroundColor: 'blue'
    }
});

// AppRegistry.registerComponent('Tabnavigator', () => Tabnavigator);

效果贴个丑图:
React-Native学习笔记之:使用Tab react-native-tab-navigator框架_第1张图片

你可能感兴趣的:(react-native)