更改默认的浅灰色背景颜色

Change the default light grey background color

默认情况下,我在 react-native 项目中创建的每个屏幕都使用非常浅的灰色作为其背景色。正如你在这里看到的:

我想知道不是为我的项目的每个屏幕设置 backgroundColor,而是在 react-native 项目中有一个全局位置,我可以为我的项目中的所有屏幕设置一次 backgroundColor ?例如,我希望我所有的屏幕都使用蓝色作为背景色。

我更喜欢使用 react-native-global-props: https://www.npmjs.com/package/react-native-global-props 非常简单:安装、导入和声明。

import { setCustomText } from 'react-native-global-props';

在最高阶组件中声明它。

const customViewProps = {
  style: {
    backgroundColor: '#d3d3d3' // light gray
  }
};

setCustomView(customViewProps);

现在它适用于您应用中的所有 View

因为你已经在使用 react-navigation 它有一个主题提供者,你可以提供一个自定义主题,代码如下所示,

import * as React from 'react';
import { NavigationContainer, DefaultTheme } from '@react-navigation/native';

const MyTheme = {
  ...DefaultTheme,
  colors: {
    ...DefaultTheme.colors,
    primary: 'rgb(255, 45, 85)',
    background:'red'
  },
};

export default function App() {
  return (
    <NavigationContainer theme={MyTheme}>{/* content */}</NavigationContainer>
  );
}

您可以查看 documentation 了解更多信息