其實剛入手作app的時候,就應該作出簡單的頂部以及底部導航欄。無奈又在忙其餘事情,致使這些如今才整理出來。html
1.頂部導航欄:react-native-scrollable-tab-view;文檔地址:https://github.com/skv-headless/react-native-scrollable-tab-viewreact
2.底部導航欄:react-navigation中的TabNavigator;文檔地址:https://reactnavigation.org/docs/navigators/tabandroid
3.一直想讓index.android.js的代碼簡潔一些,苦思不得其解,直到如今才找到了一點「路徑」,看這版的源代碼:git
index.android.js:github
/** * Sample React Native App * https://github.com/facebook/react-native * @flow */ import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Text, View, Image } from 'react-native'; //頂部導航欄 import TopTabBar from './Views/TopTabBar'; //底部導航欄 import BottomTabBar from './Views/BottomTabBar'; export default class ywg extends Component { render() { return ( <View style={{flex:1}}> <TopTabBar/> <BottomTabBar/> </View> ); } } AppRegistry.registerComponent('ywg', () => ywg);
怎樣?夠簡單吧……對了,這樣的代碼看起來才比較「優雅」(容忍zheng小葉正兒八經的胡說八道哦~)而主要的代碼就在react-native
//頂部導航欄 import TopTabBar from './Views/TopTabBar'; //底部導航欄 import BottomTabBar from './Views/BottomTabBar';
這兩個紅色的文件中。app
【重點注意】將兩個Component同時使用的時候,必定要在最外層的View上定義樣式,不然任你怎樣擺弄,它們老是不會展示「廬山真面目」,具體的文檔在:http://reactnative.cn/docs/0.46/layout-props.htmlless
這是項目文件路徑。工具
BottomTabBar.js:佈局
/** * Sample React Native App * https://github.com/facebook/react-native * @flow */ import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Text, View, Image } from 'react-native'; //底部導航欄 import { TabNavigator } from "react-navigation"; class Home extends React.Component { static navigationOptions = { tabBarLabel: '熱點', tabBarIcon: ({ focused, tintColor }) => ( <Image source={focused ? require('../Images/hot_hover.png') : require('../Images/hot.png')} style={{ width: 26, height: 26, tintColor: tintColor }} /> ) }; render() { return ( <View style={styles.container}> <Text>這是熱點</Text> </View> ); } } class Circle extends React.Component { static navigationOptions = { tabBarLabel: '圈子', tabBarIcon: ({ focused, tintColor }) => ( <Image source={focused ? require('../Images/coterie_hover.png') : require('../Images/coterie.png')} style={{ width: 26, height: 26, tintColor: tintColor }} /> ) }; render() { return ( <View style={styles.container}> <Text>這是圈子內容</Text> </View> ); } } class Tools extends React.Component { static navigationOptions = { tabBarLabel: '工具', tabBarIcon: ({ focused, tintColor }) => ( <Image source={focused ? require('../Images/tool.png') : require('../Images/tool.png')} style={{ width: 26, height: 26, tintColor: tintColor }} /> ) }; render() { return ( <View style={styles.container}> <Text>這是工具內容</Text> </View> ); } } class Mypage extends React.Component { static navigationOptions = { tabBarLabel: '個人', tabBarIcon: ({ focused, tintColor }) => ( <Image source={focused ? require('../Images/my_hover.png') : require('../Images/my.png')} style={{ width: 26, height: 26, tintColor: tintColor }} /> ) }; render() { return ( <View style={styles.container}> <Text>這是個人內容</Text> </View> ); } } const BottomTabBar = TabNavigator( { Home: { screen: Home, }, Circle: { screen: Circle, }, Tools: { screen: Tools, }, Mypage: { screen: Mypage, }, }, { tabBarOptions: { activeTintColor: '#4BC1D2', inactiveTintColor: '#000', showIcon: true, showLabel: true, upperCaseLabel: false, pressColor: '#823453', pressOpacity: 0.8, style: { backgroundColor: '#fff', paddingBottom: 0, borderTopWidth: 0.5, borderTopColor: '#ccc', }, labelStyle: { fontSize: 12, margin: 1 }, indicatorStyle: { height: 0 }, //android 中TabBar下面會顯示一條線,高度設爲 0 後就不顯示線了 }, tabBarPosition: 'bottom', swipeEnabled: false, animationEnabled: false, lazy: true, backBehavior: 'none', }); const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#fff', } }); module.exports = BottomTabBar;
TopTabBar.js:
/** * Sample React Native App * https://github.com/facebook/react-native * @flow */ import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Text, View, Image } from 'react-native'; import HomePage from '../Views/HomePage'; import PricePage from '../Views/PricePage'; import InventoryPage from '../Views/InventoryPage'; //頂部導航 var ScrollableTabView = require('react-native-scrollable-tab-view'); export default class TopTabBar extends Component { render() { return ( <ScrollableTabView tabBarUnderlineStyle={{backgroundColor:'#fff'}} > <HomePage tabLabel="首頁" /> <PricePage tabLabel="成交價" /> <InventoryPage tabLabel="庫存" /> </ScrollableTabView> ); } } module.exports = TopTabBar;
而關於這些的詳細介紹能夠參考這裏(老大的小結):http://www.cnblogs.com/vipstone/p/7516115.html?utm_source=tuicool&utm_medium=referral;
怎樣才能實現頂部欄、底部欄控制各自部分功能呢?留下來的~~~
一直想不明白,頂部導航欄跟底部導航欄同時存在的狀況下,怎樣控制各自的功能呢?因而再請教完作手機開發的同過後才恍然大悟,原來本身想的頂部導航欄根本不是頂部導航欄,簡言之就是本身把佈局搞錯了!明明只是有底部導航欄,而所謂的「頂部導航欄」也只是底部導航欄中的第一小部分裏面嵌套着一個輪播組件,纔會給人以錯覺,啊啊啊……事實真相竟然是這樣的~