做外貿網站需要什么卡西安網站快速排名提升
簡言
移動應用基本不會只由一個頁面組成。管理多個頁面的呈現、跳轉的組件就是我們通常所說的導航器(navigator)。
React Navigation 提供了簡單易用的跨平臺導航方案,在 iOS 和 Android 上都可以進行翻頁式、tab 選項卡式和抽屜式的導航布局。
React Navigation 中的視圖是原生組件,同時用到了運行在原生線程上的Animated動畫庫,因而性能表現十分流暢。此外其動畫形式和手勢都非常便于定制。
要想詳細了解 React Navigation 的具體用法,請訪問其React Navigation官網。
本篇是在window系統android環(huán)境下使用,mac系統請查看官網。
React Navigation
安裝
官網安裝指南Doc
核心包安裝命令:
yarn add @react-navigation/native
除此之外,還要安裝:
yarn add react-native-screens react-native-safe-area-context
react-native-screens 軟件包需要一個額外的配置步驟才能在安卓設備上正常運行。編輯位于 android/app/src/main/java// 下的 MainActivity.kt 或 MainActivity.java 文件:
kotlin 類型
class MainActivity: ReactActivity() {// ...override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(null)}// ...
}
或者java類型
public class MainActivity extends ReactActivity {// ...@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(null);}// ...
}
添加好后,在頂部添加新依賴包語句聲明:
import android.os.Bundle;
需要做出這一更改,以避免因視圖狀態(tài)無法在活動重啟時持續(xù)保持而導致崩潰。
使用
我們需要用 NavigationContainer 封裝整個應用程序。通常,您會在入口文件(如 index.js 或 App.js)中這樣做:
import * as React from 'react';
import { NavigationContainer } from '@react-navigation/native';export default function App() {return (<NavigationContainer>{/* Rest of your app code */}</NavigationContainer>);
}
現在就可以正常運行了。
navigator
導航器 ,上面我們只是安裝了React Navigation基礎,并沒有真正使用導航功能。
實現導航功能需要安裝對應的navigator 才可以。
6.x版本的navigator 有以下幾種:
Stack Navigator 堆棧導航器
堆棧導航器為您的應用程序提供了一種在屏幕間轉換的方法,每個新屏幕都被放置在堆棧的頂部。
這種導航器和web的history stack比較相像。
雖然 @react-navigation/stack 可定制性極強,但它是用 JavaScript 實現的。雖然它可以使用本機運行動畫和手勢,但性能可能不如本機實現的快。
安裝:
yarn add @react-navigation/stack
然后,需要安裝和配置堆棧導航器所需的庫:
yarn add react-native-gesture-handler
要最終完成 react-native-gesture-handler 的安裝,請在入口文件(如 index.js 或 App.js)的頂部添加以下內容(確保它位于頂部,且之前沒有其他內容)
import 'react-native-gesture-handler';
例子:
import { createStackNavigator } from '@react-navigation/stack';
import {Text,
} from 'react-native';
function Feed() {return <Text>top內容Feed</Text>;
}
function Settings() {return <Text>top內容Settings</Text>;
}
function Profile() {return <Text>top內容Profile</Text>;
}
const Stack = createStackNavigator();function MyStack() {return (<Stack.NavigatorinitialRouteName="Home"screenOptions={{headerMode: 'screen',headerTintColor: 'white',headerStyle: { backgroundColor: 'tomato' },}}><Stack.Screenname="Home"component={Home}options={{title: 'Awesome app',}}/><Stack.Screenname="Profile"component={Profile}options={{title: 'My profile',}}/><Stack.Screenname="Settings"component={Settings}options={{gestureEnabled: false,}}/></Stack.Navigator>);
}
Native Stack Navigator 原生堆棧導航器
導航器使用 iOS 上的 UINavigationController 和 Android 上的 Fragment 原生 API,因此使用 createNativeStackNavigator 構建的導航器將與基于這些 API 構建的應用程序具有完全相同的行為和性能特征。它還使用 react-native-web 提供基本的 Web 支持。
雖然 @react-navigation/native-stack 提供了原生性能并暴露了原生功能,如 iOS 上的大標題等,但它可能不如 @react-navigation/stack 可定制,這取決于您的需求。
安裝:
yarn add @react-navigation/native-stack
例子:
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import {Text,
} from 'react-native';
function Feed() {return <Text>top內容Feed</Text>;
}
function Settings() {return <Text>top內容Settings</Text>;
}
function Profile() {return <Text>top內容Profile</Text>;
}
const Stack = createNativeStackNavigator();function MyStack() {return (<Stack.NavigatorinitialRouteName="Home"screenOptions={{headerTintColor: 'white',headerStyle: { backgroundColor: 'tomato' },}}><Stack.Screenname="Home"component={Home}options={{title: 'Awesome app',}}/><Stack.Screenname="Profile"component={Profile}options={{title: 'My profile',}}/><Stack.Screenname="Settings"component={Settings}options={{gestureEnabled: false,}}/></Stack.Navigator>);
}
Drawer Navigator 抽屜導航器
抽屜導航器在屏幕一側顯示一個導航抽屜,可通過手勢打開或關閉。
安裝:
yarn add @react-navigation/drawer
yarn add react-native-gesture-handler react-native-reanimated
要最終完成 react-native-gesture-handler 的安裝,請在入口文件(如 index.js 或 App.js)的頂部(確保位于頂部,且之前沒有其他內容)添加以下內容
import 'react-native-gesture-handler';
react-native-reanimated安裝后,也要在babel.config.js上加下配置:
module.exports = {plugins: ['react-native-reanimated/plugin'],
};
不加在編譯時會報react-native-reanimated相關的錯誤。
例子:
import { createDrawerNavigator } from '@react-navigation/drawer';
import {Text,
} from 'react-native';
function Feed() {return <Text>top內容Feed</Text>;
}
function Notifications() {return <Text>top內容Notifications</Text>;
}
function Profile() {return <Text>top內容Profile</Text>;
}
const Drawer = createDrawerNavigator();function MyDrawer() {return (<Drawer.Navigator initialRouteName="Feed"><Drawer.Screenname="Feed"component={Feed}options={{ drawerLabel: 'Home' }}/><Drawer.Screenname="Notifications"component={Notifications}options={{ drawerLabel: 'Updates' }}/><Drawer.Screenname="Profile"component={Profile}options={{ drawerLabel: 'Profile' }}/></Drawer.Navigator>);
}
Bottom Tabs Navigator 底部Tab導航器
屏幕底部有一個簡單的標簽欄,讓你能在不同路線之間切換。路由是懶散初始化的,只有在首次聚焦時才會安裝其屏幕組件。
安裝:
yarn add @react-navigation/bottom-tabs
例子:
例子使用了react-native-vector-icons 圖標庫,使用前記得下載安裝安裝參考
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
import {Text,
} from 'react-native';
function Feed() {return <Text>top內容Feed</Text>;
}
function Notifications() {return <Text>top內容Notifications</Text>;
}
function Profile() {return <Text>top內容Profile</Text>;
}
const Tab = createBottomTabNavigator();function MyTabs() {return (<Tab.NavigatorinitialRouteName="Feed"screenOptions={{tabBarActiveTintColor: '#e91e63',}}><Tab.Screenname="Feed"component={Feed}options={{tabBarLabel: 'Home',tabBarIcon: ({ color, size }) => (<MaterialCommunityIcons name="home" color={color} size={size} />),}}/><Tab.Screenname="Notifications"component={Notifications}options={{tabBarLabel: 'Updates',tabBarIcon: ({ color, size }) => (<MaterialCommunityIcons name="bell" color={color} size={size} />),tabBarBadge: 3,}}/><Tab.Screenname="Profile"component={Profile}options={{tabBarLabel: 'Profile',tabBarIcon: ({ color, size }) => (<MaterialCommunityIcons name="account" color={color} size={size} />),}}/></Tab.Navigator>);
}
Material Bottom Tabs Navigator
Material風格的底部標簽導航器。
官網參考指南
Material Top Tabs Navigator
頂部標簽導航器
安裝:
yarn add @react-navigation/material-top-tabs react-native-tab-view
yarn add react-native-pager-view
例子:
import { createMaterialTopTabNavigator } from '@react-navigation/material-top-tabs';
import {Text,
} from 'react-native';
function Feed() {return <Text>top內容Feed</Text>;
}
function Notifications() {return <Text>top內容Notifications</Text>;
}
function Profile() {return <Text>top內容Profile</Text>;
}
const Tab = createMaterialTopTabNavigator();function MyTabs() {return (<Tab.NavigatorinitialRouteName="Feed"screenOptions={{tabBarActiveTintColor: '#e91e63',tabBarLabelStyle: { fontSize: 12 },tabBarStyle: { backgroundColor: 'powderblue' },}}><Tab.Screenname="Feed"component={Feed}options={{ tabBarLabel: 'Home' }}/><Tab.Screenname="Notifications"component={Notifications}options={{ tabBarLabel: 'Updates' }}/><Tab.Screenname="Profile"component={Profile}options={{ tabBarLabel: 'Profile' }}/></Tab.Navigator>);
}
效果
結語
這幾種navigator 可以隨意嵌套使用,具體使用方法請到官方網站閱覽。