自学React-native (第四天)-- BottomTabNavigator 和 MaterialBottomTabNavigator、DrawerNavigator导航

自学React-native (第四天)-- BottomTabNavigator 和 MaterialBottomTabNavigator、DrawerNavigator导航

1.前言:

由于使用了下边栏图标,所以安装了react-native-vector-icons,由于rnvi组件带有自己的ios/android代码所以需要使用命令:react-native link react-native-vector-icons将原生资源引入到本地项目中。影响到的地方:

  • android/settings.gradle
rootProject.name = 'lesson2'
include ':react-native-vector-icons'
project(':react-native-vector-icons').projectDir = new File(rootProject.projectDir, '..\\node_modules\\react-native-vector-icons\\android')
include ':react-native-gesture-handler'
project(':react-native-gesture-handler').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-gesture-handler/android')

include ':app'

  • ios\lesson2.xcodeproj\project.pbxproj
615C19A7FD294504B866D8AB /* libRNGestureHandler.a */ = {isa = PBXFileReference; name = "libRNGestureHandler.a"; path = "libRNGestureHandler.a"; sourceTree = "<group>"; fileEncoding = undefined; lastKnownFileType = archive.ar; explicitFileType = undefined; includeInIndex = 0; };
		F2CF6488FB2D468B9DB27127 /* RNVectorIcons.xcodeproj */ = {isa = PBXFileReference; name = "RNVectorIcons.xcodeproj"; path = "../node_modules/react-native-vector-icons/RNVectorIcons.xcodeproj"; sourceTree = "<group>"; fileEncoding = undefined; lastKnownFileType = wrapper.pb-project; explicitFileType = undefined; includeInIndex = 0; };
		4DBD5F44AA6F4D888C8DC713 /* libRNVectorIcons.a */ = {isa = PBXFileReference; name = "libRNVectorIcons.a"; path = "libRNVectorIcons.a"; sourceTree = "<group>"; fileEncoding = undefined; lastKnownFileType = archive.ar; explicitFileType = undefined; includeInIndex = 0; };
		4646F79763BD4A1681E4E82D /* libRNVectorIcons-tvOS.a */ = {isa = PBXFileReference; name = "libRNVectorIcons-tvOS.a"; path = "libRNVectorIcons-tvOS.a"; sourceTree = "<group>"; fileEncoding = undefined; lastKnownFileType = archive.ar; explicitFileType = undefined; includeInIndex = 0; };
		6F8A6F7AB3BB450BA888B4FC /* AntDesign.ttf */ = {isa = PBXFileReference; name = "AntDesign.ttf"; path = "../node_modules/react-native-vector-icons/Fonts/AntDesign.ttf"; sourceTree = "<group>"; fileEncoding = undefined; lastKnownFileType = unknown; explicitFileType = undefined; includeInIndex = 0; };
		DCE3CACDE4AA482993A611D1 /* Entypo.ttf */ = {isa = PBXFileReference; name = "Entypo.ttf"; path = "../node_modules/react-native-vector-icons/Fonts/Entypo.ttf"; sourceTree = "<group>"; fileEncoding = undefined; lastKnownFileType = unknown; explicitFileType = undefined; includeInIndex = 0; };
		E1408D85CEC641D881E848FA /* EvilIcons.ttf */ = {isa = PBXFileReference; name = "EvilIcons.ttf"; path = "../node_modules/react-native-vector-icons/Fonts/EvilIcons.ttf"; sourceTree = "<group>"; fileEncoding = undefined; lastKnownFileType = unknown; explicitFileType = undefined; includeInIndex = 0; };
		126F4C9744734DE28485F784 /* Feather.ttf */ = {isa = PBXFileReference; name = "Feather.ttf"; path = "../node_modules/react-native-vector-icons/Fonts/Feather.ttf"; sourceTree = "<group>"; fileEncoding = undefined; lastKnownFileType = unknown; explicitFileType = undefined; includeInIndex = 0; };
		285C34566016457CB2F74793 /* FontAwesome.ttf */ = {isa = PBXFileReference; name = "FontAwesome.ttf"; path = "../node_modules/react-native-vector-icons/Fonts/FontAwesome.ttf"; sourceTree = "<group>"; fileEncoding = undefined; lastKnownFileType = unknown; explicitFileType = undefined; includeInIndex = 0; };
		53DE190FADD64BF5AC104EB4 /* FontAwesome5_Brands.ttf */ = {isa = PBXFileReference; name = "FontAwesome5_Brands.ttf"; path = "../node_modules/react-native-vector-icons/Fonts/FontAwesome5_Brands.ttf"; sourceTree = "<group>"; fileEncoding = undefined; lastKnownFileType = unknown; explicitFileType = undefined; includeInIndex = 0; };
		3FE71D63045B4336B305EA4D /* FontAwesome5_Regular.ttf */ = {isa = PBXFileReference; name = "FontAwesome5_Regular.ttf"; path = "../node_modules/react-native-vector-icons/Fonts/FontAwesome5_Regular.ttf"; sourceTree = "<group>"; fileEncoding = undefined; lastKnownFileType = unknown; explicitFileType = undefined; includeInIndex = 0; };
		E41FDA73E3F24A4A8C7FF86A /* FontAwesome5_Solid.ttf */ = {isa = PBXFileReference; name = "FontAwesome5_Solid.ttf"; path = "../node_modules/react-native-vector-icons/Fonts/FontAwesome5_Solid.ttf"; sourceTree = "<group>"; fileEncoding = undefined; lastKnownFileType = unknown; explicitFileType = undefined; includeInIndex = 0; };
		D64D49EE286F44BCBEBF8686 /* Foundation.ttf */ = {isa = PBXFileReference; name = "Foundation.ttf"; path = "../node_modules/react-native-vector-icons/Fonts/Foundation.ttf"; sourceTree = "<group>"; fileEncoding = undefined; lastKnownFileType = unknown; explicitFileType = undefined; includeInIndex = 0; };
		74B156CB29204321AA8C7273 /* Ionicons.ttf */ = {isa = PBXFileReference; name = "Ionicons.ttf"; path = "../node_modules/react-native-vector-icons/Fonts/Ionicons.ttf"; sourceTree = "<group>"; fileEncoding = undefined; lastKnownFileType = unknown; explicitFileType = undefined; includeInIndex = 0; };
		ABA8981AB0604A9281D5EDCF /* MaterialCommunityIcons.ttf */ = {isa = PBXFileReference; name = "MaterialCommunityIcons.ttf"; path = "../node_modules/react-native-vector-icons/Fonts/MaterialCommunityIcons.ttf"; sourceTree = "<group>"; fileEncoding = undefined; lastKnownFileType = unknown; explicitFileType = undefined; includeInIndex = 0; };
		01198129809A49F3B2E4F067 /* MaterialIcons.ttf */ = {isa = PBXFileReference; name = "MaterialIcons.ttf"; path = "../node_modules/react-native-vector-icons/Fonts/MaterialIcons.ttf"; sourceTree = "<group>"; fileEncoding = undefined; lastKnownFileType = unknown; explicitFileType = undefined; includeInIndex = 0; };
		4E85B377CCC94C5D995AAA88 /* Octicons.ttf */ = {isa = PBXFileReference; name = "Octicons.ttf"; path = "../node_modules/react-native-vector-icons/Fonts/Octicons.ttf"; sourceTree = "<group>"; fileEncoding = undefined; lastKnownFileType = unknown; explicitFileType = undefined; includeInIndex = 0; };
		04BD12C5D8944C8DBBCFC1FF /* SimpleLineIcons.ttf */ = {isa = PBXFileReference; name = "SimpleLineIcons.ttf"; path = "../node_modules/react-native-vector-icons/Fonts/SimpleLineIcons.ttf"; sourceTree = "<group>"; fileEncoding = undefined; lastKnownFileType = unknown; explicitFileType = undefined; includeInIndex = 0; };
		0D2D64FF67544EAE80542258 /* Zocial.ttf */ = {isa = PBXFileReference; name = "Zocial.ttf"; path = "../node_modules/react-native-vector-icons/Fonts/Zocial.ttf"; sourceTree = "<group>"; fileEncoding = undefined; lastKnownFileType = unknown; explicitFileType = undefined; includeInIndex = 0; };
/* End PBXFileReference section */

2.代码展示

顾名思义,BottomTabNavigator是底部导航栏,包括图标展示的功能。MaterialTopTabNavigator是顶部导航栏,一般用于业内切换。这两个导航栏的使用方法基本和stackNavigator一样,只不过是在专用方面进行了特化,这里就仅展示以下代码,各位读者自己思考其中不同.下面是两个导航器的详细配置:

export const AppBottomTabNavigator = createBottomTabNavigator(
  {
    HomePage: {
      screen: HomePage,
      navigationOptions: {
        tabBarLabel: "最热",
        tabBarIcon: ({ tintColor, focused }) => {
          return (
            <Ionicons
              name={"ios-home"}
              size={26}
              style={{ color: tintColor }}
            />
          );
        }
      }
    },
    Page1: {
      screen: Page1,
      navigationOptions: {
        tabBarLabel: "趋势",
        tabBarIcon: ({ tintColor, focused }) => {
          console.log("tintColor:",tintColor);
          return (
            <Ionicons
              name={"ios-people"}
              size={26}
              style={{ color: tintColor }}
            />
          );
        }
      }
    },
    Page2: {
      screen: Page2,
      navigationOptions: {
        tabBarLabel: "收藏",
        tabBarIcon: ({ tintColor, focused }) => (
          <Ionicons
            name={"ios-chatboxes"}
            size={26}
            style={{ color: tintColor }}
          />
        )
      }
    },
    Page3: {
      screen: Page3,
      navigationOptions: {
        tabBarLabel: "我的",
        tabBarIcon: ({ tintColor, focused }) => (
          <Ionicons
            name={"ios-aperture"}
            size={26}
            style={{ color: tintColor }}
          />
        )
      }
    }
  },
  {
    tabBarOptions: {
      activeTintColor: "#e91e63"
    }
  }
);

在这里插入图片描述
2. MaterialTopTabNavigator

export const AppMaterialTopTabNavigator = createMaterialTopTabNavigator(
  {
    HomePage: {
      screen: HomePage,
      navigationOptions: {
        tabBarLabel: "最热"
      }
    },
    Page1: {
      screen: Page1,
      navigationOptions: {
        tabBarLabel: "趋势"
      }
    },
    Page2: {
      screen: Page2,
      navigationOptions: {
        tabBarLabel: "收藏"
      }
    },
    Page3: {
      screen: Page3,
      navigationOptions: {
        tabBarLabel: "我的"
      }
    }
  },
  {
    tabBarOptions: {
      // activeTintColor: Platform.OS === "ios" ? "#e91e63" : "#fff"
      tabStyle: { mindWidth: 50 },
      upperCaseLabel: false, //是否使标签大写,默认为true
      scrollEnabled: true, //是否支持 选项卡滚动,默认false
      style: {
        backgroundColor: "#678" //TabBar的背景色
      },
      //标识器的样式
      indicatorStyle: {
        height: 2,
        backgroundColor: "red"
      },
      labelStyle: {
        fontSize: 13,
        marginTop: 6,
        marginBottom: 6
      }
    }
  }
);


在这里插入图片描述
3. DrawerNavigator

export const AppDrawerNavigator = createDrawerNavigator(
  {
    Page4: {
      screen: Page4,
      navigationOptions: {
        drawerLabel: "Page4",
        drawerIcon: ({ tintColor }) => (
          <MaterialIcons
            name={"drafts"}
            size={24}
            style={{ color: tintColor }}
          />
        )
      }
    },
    Page5: {
      screen: Page5,
      navigationOptions: {
        drawerLabel: "Page5",
        drawerIcon: ({ tintColor }) => (
          <MaterialIcons
            name={"drafts"}
            size={24}
            style={{ color: tintColor }}
          />
        )
      }
    }
  },
  {
    initialRouteName: "Page4",
    contentOptions: {
      activeTintColor: "#e91e63"
    },
    // 自定义侧边栏
    contentComponent: (props) => (
      <ScrollView style={{ backgroundColor: "#689", flex: 1 }}>
        <SafeAreaView forceInset={{top:"always",horizontal:"never"}}>
          <DrawerItems {...props}/>
        </SafeAreaView>
      </ScrollView>
    )
  }
);


3.后记

这一章主要介绍了BottomTabNavigator 和 MaterialBottomTabNavigator以及DrawerNavigator的基本使用方式,大家有特殊配置需求的可以参看代码展示中的配置文档

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Material Top Tabs Navigator中加入筛选按钮,你可以使用TabBar的右侧属性来添加一个按钮。在这个按钮上,你可以使用一个图标或者文字来表示筛选操作。当用户点击这个按钮时,你可以在页面上弹出一个模态框或者侧边栏来显示筛选选项。 下面是一个示例代码,展示了如何在Material Top Tabs Navigator中添加一个筛选按钮: ```dart class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); final String title; @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin { TabController _tabController; @override void initState() { super.initState(); _tabController = TabController(length: 3, vsync: this); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), bottom: TabBar( controller: _tabController, tabs: [ Tab(text: 'Tab 1'), Tab(text: 'Tab 2'), Tab(text: 'Tab 3'), ], // 添加筛选按钮 actions: [ IconButton( icon: Icon(Icons.filter_list), onPressed: () { // 弹出筛选选项 // ... }, ), ], ), ), body: TabBarView( controller: _tabController, children: [ Center(child: Text('Tab 1')), Center(child: Text('Tab 2')), Center(child: Text('Tab 3')), ], ), ); } } ``` 在这个示例中,我们在TabBar的actions属性中添加了一个IconButton,用于表示筛选操作。当用户点击这个按钮时,我们可以在onPressed回调函数中弹出一个模态框或者侧边栏,来显示筛选选项。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值