react-native-tab-navigator組件的基本使用示例代碼
要做的效果很簡單,如下圖所示:
使用基本教程
1.引入組件
import TabNavigator from 'react-native-tab-navigator';
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)機(jī):
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 } })
這個(gè)時(shí)候效果已經(jīng)出來了,我們繼續(xù)抽象組件:
將每一個(gè)欄目抽出來放到一個(gè)統(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> ) }
此時(shí),render方法中就直接引用四個(gè)方法即可:
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寫法進(jìn)行設(shè)置 |
tabBarShadowStyle | inherited | object (style) | TabBar陰影的樣式,不過對于扁平化的設(shè)計(jì),這個(gè)屬性應(yīng)該用處不大 |
hidesTabTouch | false | boolean | bool類型,即是否隱藏Tab按鈕的按下效果 |
TabNavigator.Item props
prop | default | type | description |
---|---|---|---|
renderIcon | none | function | 即圖標(biāo),但為function類型,所以這里需要用到Arrow Function |
renderSelectedIcon | none | function | 選中狀態(tài)的圖標(biāo),非必填,也是function類型 |
badgeText | none | string or number | 即Tab右上角的提示文字,可為String或Number,類似QQ中Tab右上角的消息提示,非必填 |
renderBadge | none | function | 提示角標(biāo)渲染方式,function類型,類似render的使用,非必填 |
title | none | string | 標(biāo)題,String類型,非必填 |
titleStyle | inherited | style | 標(biāo)題樣式,style類型,非必填 |
selectedTitleStyle | none | style | 選中標(biāo)題樣式,style類型,非必填 |
tabStyle | inherited | style | styling for tab |
selected | none | boolean | bool型,是否選中狀態(tài),可使用setState進(jìn)行控制,默認(rèn)false |
onPress | none | function | 即點(diǎn)擊事件的回調(diào)函數(shù),這里需要控制的是state |
allowFontScaling | false | boolean | bool型,是否允許字體縮放,默認(rèn)false |
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 微信小程序間使用navigator跳轉(zhuǎn)傳值問題實(shí)例分析
- DataGridView使用BindingNavigator實(shí)現(xiàn)簡單分頁功能
- React Native中NavigatorIOS組件的簡單使用詳解
- React Native中Navigator的使用方法示例
- react-native組件中NavigatorIOS和ListView結(jié)合使用的方法
- React-Native中禁用Navigator手勢返回的示例代碼
- ReactNative頁面跳轉(zhuǎn)Navigator實(shí)現(xiàn)的示例代碼
- JavaScript navigator.userAgent獲取瀏覽器信息案例講解
相關(guān)文章
React使用highlight.js Clipboard.js實(shí)現(xiàn)代碼高亮復(fù)制
這篇文章主要為大家介紹了React使用highlight.js Clipboard.js實(shí)現(xiàn)代碼高亮復(fù)制功能示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04React+CSS?實(shí)現(xiàn)繪制豎狀柱狀圖
這篇文章主要介紹了React+CSS?實(shí)現(xiàn)繪制豎狀柱狀圖,文章圍繞主題展開詳細(xì)的內(nèi)容介紹。具有一定的參考價(jià)值,需要的朋友可以參考一下2022-09-09React Hook 父子組件相互調(diào)用函數(shù)方式
這篇文章主要介紹了React Hook 父子組件相互調(diào)用函數(shù)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09