推荐项目:React Native Material Bottom Navigation
warning:
这篇文章距离上次修改已过263天,其中的内容可能已经有所变动。
React Native Material Bottom Navigation 是一个为React Native应用程序提供Material Design风格底部导航栏的库。以下是如何使用该库的一个基本示例:
首先,安装库:
npm install @react-native-community/masked-view react-native-paper react-native-safe-area-context @react-navigation/material-bottom-tabs
然后,你可以在你的React Native项目中这样使用它:
import * as React from 'react';
import { View, Text } from 'react-native';
import { createMaterialBottomTabNavigator } from '@react-navigation/material-bottom-tabs';
import { Entypo } from '@expo/vector-icons';
import { NavigationContainer } from '@react-navigation/native';
// 定义标签页
const HomeScreen = () => (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Home!</Text>
</View>
);
const SettingsScreen = () => (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text>Settings!</Text>
</View>
);
// 创建标签导航器
const Tab = createMaterialBottomTabNavigator();
export default function App() {
return (
<NavigationContainer>
<Tab.Navigator
initialRouteName="Home"
activeColor="#f0edf6"
inactiveColor="#3e243d"
barStyle={{ backgroundColor: '#4caf50' }}
>
<Tab.Screen
name="Home"
component={HomeScreen}
options={{
tabBarIcon: ({ color }) => <Entypo name="home" color={color} size={26} />
}}
/>
<Tab.Screen
name="Settings"
component={SettingsScreen}
options={{
tabBarIcon: ({ color }) => <Entypo name="cog" color={color} size={26} />
}}
/>
</Tab.Navigator>
</NavigationContainer>
);
}
在这个例子中,我们创建了一个含有两个标签页的底部导航栏,分别是Home和Settings。每个标签页都有自己的图标,并且可以通过点击进行切换。这个示例使用了@react-navigation/material-bottom-tabs
来创建标签栏,@react-navigation/native
来管理导航,以及react-native-paper
提供的图标组件。
评论已关闭