前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ReactNative-底部TabBar react-native-tab-navigator

ReactNative-底部TabBar react-native-tab-navigator

作者头像
czjwarrior
发布2018-05-28 11:13:04
7800
发布2018-05-28 11:13:04
举报

首先需要安装 react-native-tab-navigator

npm install react-native-tab-navigator –save

导入组件

import TabNavigator from 'react-native-tab-navigator'

详细代码如下:

import React, { Component } from 'react';
import {
  AppRegistry,
  ScrollView,
  StyleSheet,
  TouchableOpacity,
  Text,
  Image,
  View,
} from 'react-native';

import { StackNavigator } from 'react-navigation';

import TabNavigator from 'react-native-tab-navigator'

export default class DemoApp extends Component {

    constructor(props){  
        super(props)  
        this.state={  
            selectedTab:'首页',  
        }  
    } 

  render() {
    return (
      <View style={styles.container}>
        <TabNavigator>
            <TabNavigator.Item
                selected={this.state.selectedTab === '首页'}
                title = '首页'
                titleStyle={styles.tabText}
                selectedTitleStyle={styles.selectedTabText} 
                renderIcon={() => <Image style={styles.icon} source={require("./images/tab_icon_home@2x.png")} />}

                renderSelectedIcon={() => <Image style={[styles.icon,{tintColor:'red'}]} source={require("./images/tab_icon_homes@2x.png")} />}
                
                onPress={() => this.setState({ selectedTab: '首页' })}
            >
                <View style={styles.page0}>
                    <Text style=>首页</Text>
                </View>
            </TabNavigator.Item>

            <TabNavigator.Item
                selected={this.state.selectedTab === '购物车'}
                title = '购物车'
                titleStyle={styles.tabText}
                selectedTitleStyle={styles.selectedTabText} 
                renderIcon={() => <Image style={styles.icon} source={require("./images/tab_icon_home@2x.png")} />}

                renderSelectedIcon={() => <Image style={[styles.icon,{tintColor:'red'}]} source={require("./images/tab_icon_homes@2x.png")} />}
                
                onPress={() => this.setState({ selectedTab: '购物车' })}
            >
                <View style={styles.page1}>
                    <Text style=>购物车</Text>
                </View>
            </TabNavigator.Item>

            <TabNavigator.Item
                selected={this.state.selectedTab === '目的地'}
                title = '目的地'
                titleStyle={styles.tabText}
                selectedTitleStyle={styles.selectedTabText} 
                renderIcon={() => <Image style={styles.icon} source={require("./images/tab_icon_destn@2x.png")} />}

                renderSelectedIcon={() => <Image style={[styles.icon,{tintColor:'red'}]} source={require("./images/tab_icon_destns@2x.png")} />}
                
                onPress={() => this.setState({ selectedTab: '目的地' })}
            >
                <View style={styles.page0}>
                    <Text style=>目的地</Text>
                </View>
            </TabNavigator.Item>

            <TabNavigator.Item
                selected={this.state.selectedTab === '我的'}
                title = '我的'
                titleStyle={styles.tabText}
                selectedTitleStyle={styles.selectedTabText} 
                renderIcon={() => <Image style={styles.icon} source={require("./images/tab_icon_usercenter@2x.png")} />}

                renderSelectedIcon={() => <Image style={[styles.icon,{tintColor:'red'}]} source={require("./images/tab_icon_usercenters@2x.png")} />}
                
                onPress={() => this.setState({ selectedTab: '我的' })}
            >
                <View style={styles.page1}>
                    <Text style=>我的</Text>
                </View>
            </TabNavigator.Item>
        </TabNavigator>
      </View>
    );
  }
}

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: 'red'
    }
});

AppRegistry.registerComponent('DemoApp', () => DemoApp);

效果图:

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-08-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档