推荐一个超牛逼的第三方库:react-navigation。
官方文档
安装
- 在 React Native 项目中安装 react-navigation 包:
$ npm install --save react-navigation
# or with yarn
# yarn add react-navigation
- 安装 react-native-gesture-handler :
$ npm install --save react-native-gesture-handler
# or with yarn
# yarn add react-native-gesture-handler
- 链接所有依赖项:
$ react-native link
项目结构
xmjg.jpg实例
1. 逻辑代码
a. 入口文件:App.js
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
*/
import React, {Component} from 'react';
import {
StyleSheet,
Image
} from 'react-native';
import {
createBottomTabNavigator,
createStackNavigator,
createDrawerNavigator,
createAppContainer
} from "react-navigation";
import Home from './src/Home';
import Type from './src/Type';
import ShopCar from './src/ShopCar';
import Mine from './src/Mine';
import Details from './src/Details';
//侧滑菜单的页面
import Wallet from "./src/drawer/Wallet";
import CardCoupons from "./src/drawer/CardCoupons";
import Invite from "./src/drawer/Invite";
/**
* 配置底部标签
*/
const Tab = createBottomTabNavigator({
//每一个页面的配置
Home: {
screen: Home,
navigationOptions: {
//stackNavigator的属性
headerTitle: '首页',
gestureResponseDistance: {horizontal: 300},
headerBackTitle: null,
headerStyle: {backgroundColor: '#EB3695'},//导航栏的样式
headerTitleStyle: {//导航栏文字的样式
color: 'white',
//设置标题的大小
fontSize: 16,
//居中显示
alignSelf: 'center',
},
//tab 的属性
tabBarLabel: '首页',
tabBarIcon: ({tintColor}) => (
<Image
source={require('./images/ic_home.png')}
style={[{height: 24, width: 24}, {tintColor: tintColor}]}/>
),
},
},
Type: {
screen: Type,
navigationOptions: {
//stackNavigator的属性
headerTitle: '分类',
gestureResponseDistance: {horizontal: 300},
headerBackTitle: null,
headerStyle: {backgroundColor: '#EB3695'},//导航栏的样式
headerTitleStyle: {//导航栏文字的样式
color: 'white',
//设置标题的大小
fontSize: 16,
//居中显示
alignSelf: 'center',
},
//tab 的属性
tabBarLabel: '分类',
tabBarIcon: ({tintColor}) => (
<Image
source={require('./images/ic_type.png')}
style={[{height: 24, width: 24}, {tintColor: tintColor}]}
/>
),
}
},
ShopCar: {
screen: ShopCar,
navigationOptions: {
//stackNavigator的属性
headerTitle: '购物车',
gestureResponseDistance: {horizontal: 300},
headerBackTitle: null,
headerStyle: {backgroundColor: '#EB3695'},//导航栏的样式
headerTitleStyle: {//导航栏文字的样式
color: 'white',
//设置标题的大小
fontSize: 16,
//居中显示
alignSelf: 'center',
},
//tab 的属性
tabBarLabel: '购物车',
tabBarIcon: ({tintColor}) => (
<Image
source={require('./images/ic_shop_car.png')}
style={[{height: 24, width: 24}, {tintColor: tintColor}]}
/>
),
}
},
Mine: {
screen: Mine,
navigationOptions: {
//stackNavigator的属性
headerTitle: '我的',
gestureResponseDistance: {horizontal: 300},
headerBackTitle: null,
headerStyle: {backgroundColor: '#EB3695'},//导航栏的样式
headerTitleStyle: {//导航栏文字的样式
color: 'white',
//设置标题的大小
fontSize: 16,
//居中显示
alignSelf: 'center',
},
//tab 的属性
tabBarLabel: '我的',
tabBarIcon: ({tintColor}) => (
<Image
source={require('./images/ic_me.png')}
style={[{height: 24, width: 24}, {tintColor: tintColor}]}
/>
),
}
},
}, {
//设置TabNavigator的位置
tabBarPosition: 'bottom',
//是否在更改标签时显示动画
animationEnabled: true,
//是否允许在标签之间进行滑动
swipeEnabled: true,
//按 back 键是否跳转到第一个Tab(首页), none 为不跳转
backBehavior: "none",
//设置Tab标签的属性
tabBarOptions: {
//Android属性
upperCaseLabel: false,//是否使标签大写,默认为true
//共有属性
showIcon: true,//是否显示图标,默认关闭
showLabel: true,//是否显示label,默认开启
activeTintColor: '#EB3695',//label和icon的前景色 活跃状态下(选中)
inactiveTintColor: 'gray',//label和icon的前景色 活跃状态下(未选中)
style: { //TabNavigator 的背景颜色
backgroundColor: 'white',
height: 55,
},
indicatorStyle: {//标签指示器的样式对象(选项卡底部的行)。安卓底部会多出一条线,可以将height设置为0来暂时解决这个问题
height: 0,
},
labelStyle: {//文字的样式
fontSize: 13,
marginTop: -5,
marginBottom: 5,
},
iconStyle: {//图标的样式
marginBottom: 5,
}
},
});
/*
* 配置堆栈导航
*/
const Stack = createStackNavigator({
Tab: {
screen: Tab,
},
Details: {
screen: Details,
},
//DrawerNavigator跳转的页面也必须在这里注册
Wallet: {
screen: Wallet,
},
CardCoupons: {
screen: CardCoupons,
},
Invite: {
screen: Invite,
},
});
/**
* 配置侧滑菜单
*/
const Drawer = createDrawerNavigator({
Home: {
screen: Stack,
navigationOptions: {
drawerLabel: '首页',
drawerIcon: ({tintColor}) => (
<Image
source={require('./images/ic_home.png')}
style={[styles.icon, {tintColor: tintColor}]}
/>
),
}
},
Wallet: {
screen: Wallet,
navigationOptions: {
drawerLabel: '我的钱包',
drawerIcon: ({tintColor}) => (
<Image
source={require('./images/wallet.png')}
style={[styles.icon, {tintColor: tintColor}]}
/>
),
}
},
CardCoupons: {
screen: CardCoupons,
navigationOptions: {
drawerLabel: '我的卡券',
drawerIcon: ({tintColor}) => (
<Image
source={require('./images/cardcoupons.png')}
style={[styles.icon, {tintColor: tintColor}]}
/>
),
}
},
Invite: {
screen: Invite,
navigationOptions: {
drawerLabel: '邀请好友',
drawerIcon: ({tintColor}) => (
<Image
source={require('./images/invite.png')}
style={[styles.icon, {tintColor: tintColor}]}
/>
),
}
},
}, {
drawerWidth: 250, // 展示的宽度
drawerPosition: 'left', // 抽屉在左边还是右边
// contentOptions: {
// // activeTintColor: 'black', // 选中文字颜色
// activeBackgroundColor: '#fff', // 选中背景颜色
// inactiveTintColor: '#EB3695', // 未选中文字颜色
// inactiveBackgroundColor: '#fff', // 未选中背景颜色
// style: { // 样式
//
// }
// },
});
const AppContainer = createAppContainer(Drawer);
export default class App extends Component {
render() {
return (
<AppContainer />
);
}
}
const styles = StyleSheet.create({
icon: {
width: 24,
height: 24,
}
});
b. 各个子界面类似,所以给出一个: Home.js
import React, {Component} from 'react';
import {
StyleSheet,
Text,
View,
TouchableOpacity
} from 'react-native';
export default class Home extends Component {
render() {
return (
<View style={styles.container}>
<TouchableOpacity
style={styles.button}
activeOpacity={0.5}
onPress={() => {
this.props.navigation.navigate('Details');
}}
>
<Text style = {styles.text}>首页</Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
text: {
color:'white'
},
button: {
width: 120,
height: 45,
borderRadius: 5,
alignItems: 'center',
justifyContent: 'center',
backgroundColor:'#4398ff'
},
});
c. 详情页,也就是首页按钮点击进去的界面以及左边拉出的菜单栏相应的界面:Details.js
import React, {Component} from 'react';
import {
StyleSheet,
Text,
View
} from 'react-native';
export default class Details extends Component {
static navigationOptions = ({ navigation, screenProps }) => ({
headerTitle: '详情',
gestureResponseDistance: { horizontal: 300 },
headerBackTitle: null,
headerStyle: styles.headerStyle,
headerTitleStyle: styles.headerTitleStyle,
headerTintColor: 'white',
headerRight: (<View/>),
});
render() {
return (
<View style={styles.container}>
<Text style={styles.text}>我是首页跳转过来的页面</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
text: {
color:'black'
},
button: {
width: 240,
height: 45,
borderRadius: 5,
alignItems: 'center',
justifyContent: 'center',
backgroundColor:'#4398ff'
},
headerStyle: {
backgroundColor: '#EB3695',
},
headerTitleStyle: {
color: 'white',
fontSize: 18,
alignSelf: 'center',
},
});
2. 效果图
网友评论