选项卡react-native-scrollable-tab-view(进阶篇)
http://www.jianshu.com/p/b0cfe7f11ee7 在上一篇文章当中,我们学习了react-native-scrollable-tab-view的基本使用方式,包括基本Props的使用介绍等。我们知道官方为我们提供了两种基本的Tab控制器样式,DefaultTabBar和ScrollableTabBar。很多情况下,官方的样式并不能满足我们的需求(备注:官方的样式是文字+下划线的风格),那么此时就需要我们自己来实现特定的样式。 今天我们是React-native-scrollable-tab-view的下篇,本片文章的很多概念建立在上篇的基础之上,还没有阅读过的朋友可以先去参考[React Native]react-native-scrollable-tab-view(入门篇)。 本文要实现这样的效果:
preview.gif
一、准备工作 1.新建一个项目 react-native init Demo7
2.添加react-native-scrollable-tab-view npm install react-native-scrollable-tab-view --save
3.添加react-native-vector-icons > npm install react-native-vector-icons --save
> rnpm link
rnpm是一个React Native包管理器,我们也可以通过编辑 apply from: "../../node_modules/react-native-vector-icons/fonts.gradle"
react-native-vector-icons介绍:
有趣的是,这个库的图标来源有很多,下面大概列举了一些:
其中用的最多的是Ionicons,所以本篇文章的图标来源也就选择它了。
其中,
ionicons.png
search.png
result.png
二、开始工作 首先,自定义一个TabBar组件你需要知道以下几个点:
好了,介绍完要点,我们就开始编写TabBar组件了。 import Icon from 'react-native-vector-icons/Ionicons';
2.我们希望每个Tab的图标和名称都是外部组件通过 propTypes = {
...
tabNames: React.PropTypes.array,161);">// 保存Tab名称
tabIconNames: React.PropTypes.array,161);">// 保存Tab图标
}
3.实现 render() {
return (
<View style={styles.tabs}>
{this.props.tabs.map((tab,i) => this.renderTabOption(tab,i))}
</View>
);
}
这个方法很简单,返回一个容器View,容器View内包含的子View是通过遍历 4.实现 renderTabOption(tab,i) {
const color = this.props.activeTab == i? "#6B8E23" : "#ADADAD"; // 判断i是否是当前选中的tab,设置不同的颜色
return (
<TouchableOpacity onPress={()=>this.props.goToPage(i)} style={styles.tab}> View style={styles.tabItem}> Icon name={this.props.tabIconNames[i]} // 图标 size={30} color={color}/> Text style={{color: color}}> {this.props.tabNames[i]} </Text> View> TouchableOpacity> ); }
这个方法应该也比较简单,使用 5.使用 > import ScrollableTabView 'react-native-scrollable-tab-view'
> import WeixinTabBar './WeixinTabBar'
我们最终实现的效果图有4个 constructor(props) {
super(props);
this.state = {
tabNames: ['Tab1','Tab2',152);">'Tab3',152);">'Tab4'],tabIconNames: ['ios-paper',152);">'ios-albums',152);">'ios-paper-plane',152);">'ios-person-add'],};
}
最后,实现 render() {
let tabNames = this.state.tabNames;
let tabIconNames = this.state.tabIconNames;
return (
ScrollableTabView renderTabBar={() => WeixinTabBar tabNames={tabNames} tabIconNames={tabIconNames}/>} tabBarPosition='bottom'> {styles.content} tabLabel='key1'> Text>#1'key2'> Text>#2'key3'> Text>#3'key4'> Text>#4ScrollableTabView> ); }
其中 最后,感谢大家耐心看完本篇文章~ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |