加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ReactNative学习十-Tab-Navigator

发布时间:2020-12-15 03:35:39 所属栏目:百科 来源:网络整理
导读:1.github地址 https://github.com/exponentjs/react-native-tab-navigator 2.package.json配置 "dependencies": { "react-native-tab-navigator": "^0.2.15", } 3.说明 A tab bar that switches between scenes,written in JS for cross-platform support. I

1.github地址

https://github.com/exponentjs/react-native-tab-navigator

2.package.json配置

"dependencies": {
"react-native-tab-navigator": "^0.2.15",
}

3.说明

A tab bar that switches between scenes,written in JS for cross-platform support. It works on iOS and Android.This component is compatible with React Native 0.16 and newer.The look and feel is slightly different than the native navigator but it is better in some ways. Also it is pure JavaScript.The API of this component may change in the future to be more like Navigator's,which works great once there is better support for nested Navigators in React Native.

4.Usage

Import TabNavigator as a JavaScript module:

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

This is an example of how to use the component and some of the commonly used props that it supports:

<TabNavigator> <TabNavigator.Item selected={this.state.selectedTab === 'home'} title="Home" renderIcon={() => <Image source={...} />} renderSelectedIcon>} badgeText"1" onPress=> this.setState({ selectedTab: 'home' })}> {homeView} </TabNavigator.Item'profile'} title"Profile" renderIcon>} renderBadge<CustomBadgeView >} onPress'profile' })}> {profileView} > /TabNavigator>

See TabNavigatorItem's supported props for more info.

Hiding the Tab Bar

You can hide the tab bar by using styles. For example:

let tabBarHeight = 0; <TabNavigator tabBarStyle={{ height: tabBarHeight,overflow'hidden' }} sceneStyle={{ paddingBottom: tabBarHeight }} >

5.完整代码

'use strict';

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

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

const HOME = 'home';
const HOME_NORMAL = require('./images/tabs/home_normal.png');
const HOME_FOCUS = require('./images/tabs/home_focus.png');

const CATEGORY = 'category';
const CATEGORY_NORMAL = require('./images/tabs/category_normal.png');
const CATEGORY_FOCUS = require('./images/tabs/category_focus.png');

const FAXIAN = 'faxian';
const FAXIAN_NORMAL = require('./images/tabs/faxian_normal.png');
const FAXIAN_FOCUS = require('./images/tabs/faxian_focus.png');

const CART = 'cart';
const CART_NORMAL = require('./images/tabs/cart_normal.png');
const CART_FOCUS = require('./images/tabs/cart_focus.png');

const PERSONAL = 'personal';
const PERSONAL_NORMAL = require('./images/tabs/personal_normal.png');
const PERSONAL_FOCUS = require('./images/tabs/personal_focus.png');

export default class MainScreen extends Component {

    constructor(props) {
        super(props);
        this.state = {selectedTab: HOME}
    }

    _renderTabItem(img,selectedImg,tag,childView) {
        return (
            <TabNavigator.Item
                selected={this.state.selectedTab === tag}
                renderIcon={() => <Image style={styles.tabIcon} source={img}/>}
                renderSelectedIcon={() => <Image style={styles.tabIcon} source={selectedImg}/>}
                onPress={() => this.setState({ selectedTab: tag })}>
                {childView}
            </TabNavigator.Item>
        );
    }

    static _createChildView(tag) {
        return (
            <View style={{flex:1,backgroundColor:'#00baff',alignItems:'center',justifyContent:'center'}}>
                <Text style={{fontSize:22}}>{tag}</Text>
            </View>
        )
    }

    render() {
        return (
            <View style={{flex: 1}}>
                <Header />
                <TabNavigator hidesTabTouch={true} tabBarStyle={styles.tab}>
                    {this._renderTabItem(HOME_NORMAL,HOME_FOCUS,HOME,MainScreen._createChildView(HOME))}
                    {this._renderTabItem(CATEGORY_NORMAL,CATEGORY_FOCUS,CATEGORY,MainScreen._createChildView(CATEGORY))}
                    {this._renderTabItem(FAXIAN_NORMAL,FAXIAN_FOCUS,FAXIAN,MainScreen._createChildView(FAXIAN))}
                    {this._renderTabItem(CART_NORMAL,CART_FOCUS,CART,MainScreen._createChildView(CART))}
                    {this._renderTabItem(PERSONAL_NORMAL,PERSONAL_FOCUS,PERSONAL,MainScreen._createChildView(PERSONAL))}
                </TabNavigator>
            </View >
        );
    }
}

const styles = StyleSheet.create({
    tab: {
        height: 52,backgroundColor: '#303030',alignItems: 'center',},tabIcon: {
        width: 30,height: 35,resizeMode: 'stretch',marginTop: 12.5
    }
});

转载出处: http://www.52php.cn/article/p-uulzskdg-bgm.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读