你可以使用 React Native 状态栏(详细说明请参见此处)。你需要做的就是用一个视图包装导航器并在其上方添加一个 StatusBar 组件。不要忘记从 'react-native' 包中导入 StatusBar。
<View>
<StatusBar
backgroundColor="blue"
barStyle="light-content"
/>
<Navigator
initialRoute={{statusBarHidden: true}}
renderScene={(route, navigator) =>
<View>
<StatusBar hidden={route.statusBarHidden} />
...
</View>
}
/>
</View>
我注意到的一件事是,你应该用flex:1样式来设置父视图,如果没有它,你将只会看到一个白色的空白屏幕。尽管这在React Native文档中没有提到。
import {StatusBar} from 'react-native';
render() {
return <View>
<StatusBar
backgroundColor="#264d9b"
barStyle="light-content"
/>
... //Your code
</View>
}
reference: https://facebook.github.io/react-native/docs/statusbar
可以的:
import {StatusBar} from 'react-native';
componentDidMount() {
StatusBar.setBarStyle( 'light-content',true)
StatusBar.setBackgroundColor("#0996AE")
}
android/src/main/res/values/styles.xml
文件(模板在此处:https://github.com/facebook/react-native/blob/master/local-cli/generator-android/templates/src/app/src/main/res/values/styles.xml),并在此处阅读更多信息:https://developer.android.com/training/material/theme.html。androidStatusBarColor="#34495e"
将 color.xml 添加到 ..android/app/src/main/res/values 目录下,然后粘贴以下代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- color for the app bar and other primary UI elements -->
<color name="colorPrimary">#3F51B5</color>
<!-- a darker variant of the primary color, used for
the status bar (on Android 5.0+) and contextual app bars -->
<color name="colorPrimaryDark">#A52D53</color>
<!-- a secondary color for controls like checkboxes and text fields -->
<color name="colorAccent">#FF4081</color>
</resources>
请将以下代码复制并粘贴到..android/app/src/main/res/values/styles.xml文件中:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
{
"expo": {
"sdkVersion": "Your given Version",
"androidStatusBar": {
"backgroundColor": "#4e2ba1" (Your desirable android Status Bar Color before the app loads)
}
}
}
然后进入您的主组件或App.js,在'react-native'中导入 'StatusBar'。然后在返回中添加以下代码:
return(
<View style={{flex: 1}}> (Do not forget to style flex as 1)
<StatusBar translucent backgroundColor="rgba(0,0,0,0.2)"/>
<Your Code>
</View>
);
目前还没有公开的 API。这只能在 Android 5.0 及以上版本中使用。 正在开发桥接模块以实现相同的功能。稍后会通知您。
只需将以下代码添加到您的 class 组件中的 App.js 文件中。
componentDidMount(){
StatusBar.setBarStyle( 'light-content',true)
StatusBar.setBackgroundColor("Your color Hex-code here")
}
并将以下内容添加到您的导入语句中。
import {StatusBar} from 'react-native';