小編給大家分享一下react-native-tab-navigator組件怎么用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
要做的效果很簡單,如下圖所示:
使用基本教程
1.引入組件
import TabNavigator from 'react-native-tab-navigator';
Github上的地址
2.render方法中返回:
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/movie_gray.png")} />} renderSelectedIcon={() => <Image style={styles.icon} source={require("../images/movie_red.png")} />} onPress={() => this.setState({ selectedTab: '電影' })}> <MoviePage/> // 這里放入頁面組件 </TabNavigator.Item> <TabNavigator.Item selected={this.state.selectedTab === '音樂'} title="音樂" titleStyle={styles.tabText} selectedTitleStyle={styles.selectedTabText} renderIcon={() => <Image style={styles.icon} source={require("../images/music_gray.png")} />} renderSelectedIcon={() => <Image style={styles.icon} source={require("../images/music_red.png")} />} onPress={() => this.setState({ selectedTab: '音樂' })}> <MusicPage /> </TabNavigator.Item> <TabNavigator.Item selected={this.state.selectedTab === '圖書'} title="圖書" titleStyle={styles.tabText} selectedTitleStyle={styles.selectedTabText} renderIcon={() => <Image style={styles.icon} source={require("../images/book_gray.png")} />} renderSelectedIcon={() => <Image style={styles.icon} source={require("../images/book_red.png")} />} onPress={() => this.setState({ selectedTab: '圖書' })}> <BookPage /> </TabNavigator.Item> <TabNavigator.Item selected={this.state.selectedTab === '我的'} title="我的" titleStyle={styles.tabText} selectedTitleStyle={styles.selectedTabText} renderIcon={() => <Image style={styles.icon} source={require("../images/my_gray.png")} />} renderSelectedIcon={() => <Image style={styles.icon} source={require("../images/my_red.png")} />} onPress={() => this.setState({ selectedTab: '我的' })}> <MyPage /> </TabNavigator.Item> </TabNavigator> </View> ); }
引入頁面組件:
import MoviePage from './pages/MoviePage'; import MusicPage from './pages/MusicPage'; import BookPage from './pages/BookPage'; import MyPage from './pages/MyPage';
設(shè)置state狀態(tài)機:
constructor(props){ super(props); this.state = { selectedTab:'電影' } }
引入基本樣式:
const styles = StyleSheet.create({ container:{ flex:1, backgroundColor:'#fff' }, tabText:{ color:'#000000', fontSize:10 }, selectedTabText:{ color:'#D81E06' }, icon:{ width:20, height:20 } })
這個時候效果已經(jīng)出來了,我們繼續(xù)抽象組件:
將每一個欄目抽出來放到一個統(tǒng)一的方法中:
_renderTabarItems(selectedTab,icon,selectedIcon,Component){ return ( <TabNavigator.Item selected={this.state.selectedTab === selectedTab} title={selectedTab} titleStyle={styles.tabText} selectedTitleStyle={styles.selectedTabText} renderIcon={() => <Image style={styles.icon} source={icon} />} renderSelectedIcon={() => <Image style={styles.icon} source={selectedIcon} />} onPress={() => this.setState({ selectedTab: selectedTab })} > <Component /> </TabNavigator.Item> ) }
此時,render方法中就直接引用四個方法即可:
render() { return ( <View style={styles.container}> <TabNavigator> {this._renderTabarItems('電影',require('../img/movie_gray.png'),require('../img/movie_red.png'),MoviePage)} {this._renderTabarItems('音樂',require('../img/music_gray.png'),require('../img/music_red.png'),MusicPage)} {this._renderTabarItems('圖書',require('../img/book_gray.png'),require('../img/book_red.png'),BookPage)} {this._renderTabarItems('我的',require('../img/my_gray.png'),require('../img/my_red.png'),MyPage)} </TabNavigator> </View> ); }
至此,已經(jīng)初步完成。
組件的屬性集合:
Props
TabNavigator props
prop | default | type | description |
---|---|---|---|
sceneStyle | inherited | object (style) | 場景樣式,即Tab頁容器的樣式,可按View的style設(shè)置 |
tabBarStyle | inherited | object (style) | TabBar的樣式,基本也可按照普通的style寫法進行設(shè)置 |
tabBarShadowStyle | inherited | object (style) | TabBar陰影的樣式,不過對于扁平化的設(shè)計,這個屬性應(yīng)該用處不大 |
hidesTabTouch | false | boolean | bool類型,即是否隱藏Tab按鈕的按下效果 |
TabNavigator.Item props
prop | default | type | description |
---|---|---|---|
renderIcon | none | function | 即圖標,但為function類型,所以這里需要用到Arrow Function |
renderSelectedIcon | none | function | 選中狀態(tài)的圖標,非必填,也是function類型 |
badgeText | none | string or number | 即Tab右上角的提示文字,可為String或Number,類似QQ中Tab右上角的消息提示,非必填 |
renderBadge | none | function | 提示角標渲染方式,function類型,類似render的使用,非必填 |
title | none | string | 標題,String類型,非必填 |
titleStyle | inherited | style | 標題樣式,style類型,非必填 |
selectedTitleStyle | none | style | 選中標題樣式,style類型,非必填 |
tabStyle | inherited | style | styling for tab |
selected | none | boolean | bool型,是否選中狀態(tài),可使用setState進行控制,默認false |
onPress | none | function | 即點擊事件的回調(diào)函數(shù),這里需要控制的是state |
allowFontScaling | false | boolean | bool型,是否允許字體縮放,默認false |
以上是“react-native-tab-navigator組件怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計公司行業(yè)資訊頻道!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)頁標題:react-native-tab-navigator組件怎么用-創(chuàng)新互聯(lián)
網(wǎng)站鏈接:http://aaarwkj.com/article6/jcpog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、自適應(yīng)網(wǎng)站、靜態(tài)網(wǎng)站、企業(yè)網(wǎng)站制作、Google、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容