一.前期准备
1.导航使用的是react-navigation导航,安装方式如下:
yarn add react-navigation
npm install react-navigation --save
2.创建目录
对应底部三个tabbar
二.开始编写tabbar
1.依次如下方式编写三个底部tabbar
import React,{
Component} from 'react';
import {
View,Text} from 'react-native';
export default class Home extends Component{
render(){
return (
<View>
<Text>Home</Text>
</View>
)
}
}
2.在App.js中用createBottomTabNavigation引入
createBottomTabNavigation介绍:
https://reactnavigation.org/docs/zh-Hans/tab-based-navigation.html
在App.js中导入并引用
遇到的坑
1.报错:
react-native-gesture-handler ,react-navigation-tabs,react-native-reanimated
解决办法:各自安装即可
npm i 包名 -S
2.还有提示要有createAppContainer
解决办法引入createAppContainer
修改后的App.js代码变为:
import React from 'react';
import {
createAppContainer} from 'react-navigation';
import {
createBottomTabNavigator} from 'react-navigation-tabs';
import HomeScreen from './src/components/home/home.js';
import MineScreen from './src/components/mine/mine.js';
import MovieScreen from './src/components/movie/movie.js';
const TabBar = createBottomTabNavigator({
Home: HomeScreen,
Mine: MineScreen,
Movie: MovieScreen,
});
export default createAppContainer(TabBar);
最后,终于见证奇迹啦
三.美化tabbar
增加图标和修改样式
import React from 'react';
import {
createAppContainer} from 'react-navigation';
import {
createBottomTabNavigator} from 'react-navigation-tabs';
import HomeScreen from './src/components/home/home.js';
import MineScreen from './src/components/mine/mine.js';
import MovieScreen from './src/components/movie/movie.js';
import {
Image} from 'react-native';
const TabBar = createBottomTabNavigator(
// 里面是两个对象
{
Home: {
screen: HomeScreen,
navigationOptions: {
tabBarLabel: '首页',
tabBarIcon: ({
focused, tintColor}) => {
//跟一个方法,参数是一个对象
return (
<Image
source={
focused
? require('./src/statics/images/tarBar/home_selected.png')
: require('./src/statics/images/tarBar/home_normal.png')
}
style={
{
tintColor: tintColor, width: 25, height: 25}}
/>
);
},
},
},
Movie: {
screen: MovieScreen,
navigationOptions: {
tabBarLabel: '电影',
tabBarIcon: ({
focused, tintColor}) => {
//跟一个方法,参数是一个对象
return (
<Image
source={
focused
? require('./src/statics/images/tarBar/movie_selected.png')
: require('./src/statics/images/tarBar/movie_normal.png')
}
style={
{
tintColor: tintColor, width: 25, height: 25}}
/>
);
},
},
},
Mine: {
screen: MineScreen,
navigationOptions: {
tabBarLabel: '我的',
tabBarIcon: ({
focused, tintColor}) => {
//跟一个方法,参数是一个对象
return (
<Image
source={
focused
? require('./src/statics/images/tarBar/mine_selected.png')
: require('./src/statics/images/tarBar/mine_normal.png')
}
style={
{
tintColor: tintColor, width: 25, height: 25}}
/>
);
},
},
},
},
{
tabBarOptions: {
activeTintColor: 'black',
inactiveTintColor: 'gray',
},
},
);
export default createAppContainer(TabBar);
PS:width:25,记住这些样式是不要填写px的.
效果:
四.增加顶部导航
安装
npm i react-navigation-stack -S
最后效果:
五.轮播图制作
包名:react-native-looped-carousel
先安装,并导入
//安装
npm install react-native-looped-carousel --save
//导入
import Carousel from 'react-native-looped-carousel';
使用:
{
/* 2.轮播图 */}
<View style={
this.state.size} onLayout={
this._onLayoutDidChange}>
<Carousel
delay={
3000}
style={
this.state.size}
autoplay
// pageInfo
arrows={
true}
leftArrowText="<"
leftArrowStyle={
{
color: 'white', fontSize: 30, margin: 20}}
rightArrowStyle={
{
color: 'white', fontSize: 30, margin: 20}}
rightArrowText="