如何在不编辑 Android 特定文件的情况下,更改来自 react-native
的 StatusBar
组件的背景颜色?
文档表示,可以使用backgroundColor
属性。但它失败了。 barStyle
属性、setBarStyle
和 setBackgroundColor
静态方法也无法正常工作。
只有 hidden
属性有效。
我正在使用由 create-react-native-app
建立的 Expo。
如何在不编辑 Android 特定文件的情况下,更改来自 react-native
的 StatusBar
组件的背景颜色?
文档表示,可以使用backgroundColor
属性。但它失败了。 barStyle
属性、setBarStyle
和 setBackgroundColor
静态方法也无法正常工作。
只有 hidden
属性有效。
我正在使用由 create-react-native-app
建立的 Expo。
app.json
:{
"expo": {
"sdkVersion": "16.0.0",
"androidStatusBar": {
"barStyle": "dark-content",
"backgroundColor": "#0A48A5"
}
}
}
将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>
import {StatusBar} from 'react-native';
const bar = ()=>{
return( <StatusBar backgroundColor="insert your color here"/> );
};
<StatusBar barStyle="light-content" backgroundColor="#1976D2"/>
结合使用才有效。谢谢! - Maxim Romanyuk