欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

react-native-tab-navigator組件的基本使用示例代碼

 更新時(shí)間:2017年09月07日 14:23:37   作者:隨遇而安_2750  
本篇文章主要介紹了react-native-tab-navigator組件的基本使用示例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下

要做的效果很簡單,如下圖所示:

使用基本教程

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)機(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • React-Native中props具體使用詳解

    React-Native中props具體使用詳解

    本篇文章主要介紹了React-Native中props具體使用詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-09-09
  • React路由封裝的實(shí)現(xiàn)淺析

    React路由封裝的實(shí)現(xiàn)淺析

    路由是React項(xiàng)目中相當(dāng)重要的概念,對于功能較為復(fù)雜的網(wǎng)頁來說,必然會涉及到不同功能間的頁面跳轉(zhuǎn),本篇文章將對React官方維護(hù)的路由庫React-Router-Dom的使用和常用組件進(jìn)行講解,同時(shí)對路由組件傳遞param參數(shù)的方式進(jìn)行講解,希望對各位讀者有所參考
    2022-08-08
  • React配置子路由的實(shí)現(xiàn)

    React配置子路由的實(shí)現(xiàn)

    本文主要介紹了React配置子路由的實(shí)現(xiàn),我們來通過一個(gè)簡單的例子解釋一下如何配置子路由,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • React使用highlight.js Clipboard.js實(shí)現(xiàn)代碼高亮復(fù)制

    React使用highlight.js Clipboard.js實(shí)現(xiàn)代碼高亮復(fù)制

    這篇文章主要為大家介紹了React使用highlight.js Clipboard.js實(shí)現(xiàn)代碼高亮復(fù)制功能示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • React+CSS?實(shí)現(xiàn)繪制豎狀柱狀圖

    React+CSS?實(shí)現(xiàn)繪制豎狀柱狀圖

    這篇文章主要介紹了React+CSS?實(shí)現(xiàn)繪制豎狀柱狀圖,文章圍繞主題展開詳細(xì)的內(nèi)容介紹。具有一定的參考價(jià)值,需要的朋友可以參考一下
    2022-09-09
  • React如何避免子組件無效刷新

    React如何避免子組件無效刷新

    這篇文章主要介紹了React幾種避免子組件無效刷新的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • React Native 截屏組件的示例代碼

    React Native 截屏組件的示例代碼

    本篇文章主要介紹了React Native 截屏組件的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-12-12
  • 記一個(gè)React.memo引起的bug

    記一個(gè)React.memo引起的bug

    memo可以自己決定是否更新,但它是一個(gè)函數(shù)組件而非一個(gè)類,本文主要介紹了React.memo引起的bug,具有一定的參考價(jià)值,感興趣的可以了解一下
    2022-03-03
  • React Hook 父子組件相互調(diào)用函數(shù)方式

    React Hook 父子組件相互調(diào)用函數(shù)方式

    這篇文章主要介紹了React Hook 父子組件相互調(diào)用函數(shù)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • React實(shí)現(xiàn)動效彈窗組件

    React實(shí)現(xiàn)動效彈窗組件

    最近在使用react開發(fā)項(xiàng)目,遇到這樣一個(gè)需求實(shí)現(xiàn)一個(gè)帶有動效的 React 彈窗組件,如果不考慮動效,很容易實(shí)現(xiàn),接下來小編通過本文給大家介紹React實(shí)現(xiàn)動效彈窗組件的實(shí)現(xiàn)代碼,一起看看吧
    2021-06-06

最新評論