zoukankan      html  css  js  c++  java
  • react-native + react-native-tab-navigator 实现 TabBar

    1.安装 react-native-tab-navigator

    yarn add react-native-tab-navigator

    2.页面调用

    /**
     * 主页面
     */
    import React, {Component} from 'react';
    import {
      View,
      Image,
      StyleSheet
    } from 'react-native';
    // 底部导航栏
    import TabNavigator from 'react-native-tab-navigator';
    // 首页
    import HomePage from './HomePage';
    // 购物车
    import ShopCarPage from './ShopCarPage';
    // 我的
    import MinePage from './MinePage';
    // 图片资源
    import { images } from '../../res';
    
    const dataSource = [
      {
        icon:images.tabbar_home_normal,
        selectedIcon:images.tabbar_home_selected,
        tabPage:'Home',
        tabName:'首页',
        component:HomePage
      },
      {
        icon:images.tabbar_shopcar_normal,
        selectedIcon:images.tabbar_shopcar_selected,
        tabPage:'ShopCar',
        tabName:'购物车',
        component:ShopCarPage
      },
      {
        icon:images.tabbar_mine_normal,
        selectedIcon:images.tabbar_mine_selected,
        tabPage:'Mine',
        tabName:'我的',
        component:MinePage
      }
    ];
    
    var navigation = null;
    
    export default class MainPage extends Component {
    
      constructor(props) {
        super(props);
        navigation = this.props.navigation;
        this.state = {
          selectedTab:'Home'
        };
      }
    
      render() {
    
        let tabViews = dataSource.map((item,i) => {
          return (
              <TabNavigator.Item
                title={item.tabName}
                selected={this.state.selectedTab===item.tabPage}
                titleStyle={{color:'#999999'}}
                selectedTitleStyle={{color:'#ED5100'}}
                renderIcon={()=><Image style={styles.tabIcon} source={item.icon}/>}
                renderSelectedIcon = {() => <Image style={styles.tabIcon} source={item.selectedIcon}/>}
                tabStyle={{alignSelf:'center'}}
                onPress = {() => {this.setState({selectedTab:item.tabPage})}}
                key={i}
                >
                <item.component navigation={navigation}/>
            </TabNavigator.Item>
          );
        });
    
        return (
          <View style={styles.container}>
            <TabNavigator
              hidesTabTouch={true}
              >
                {tabViews}
            </TabNavigator>
          </View>
        );
      }  
    }
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        backgroundColor: '#F5FCFF',
      },
      tabIcon:{
        23,
        height:23,
      }
    });

    3.效果图

  • 相关阅读:
    视图转换例子实践
    Xml序列化和反序列化对象使用MemoryStream实践
    iOS中控制器的实践和学习(2)认识XCode4模版(A1,A3,B2简易图)
    iOS中控制器的实践和学习(1)抛出UI问题
    拖动雪花视图实例学习
    <海量数据库解决方案>2011051901
    <海量数据库解决方案>2011051301
    提示(警告)视图的简单应用
    Delphi Prism Visual Studio Pascal For .NET
    Migrating Delphi Applications to .NET
  • 原文地址:https://www.cnblogs.com/crazycode2/p/9536507.html
Copyright © 2011-2022 走看看