Change the default light grey background color

北战南征 提交于 2020-12-15 05:43:05

问题


By default, every screen I created in react-native project use a very light grey color as its background color. As you see here:

I wonder instead of I set backgroundColor for each screen of my project, is there a global place in react-native project that I can set the backgroundColor once for all screens in my project? For example, I would like all my screens use blue color as background color.


回答1:


As you are already using react-navigation it has a theme provider which you can provide a custom theme, the code would be like below,

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>
  );
}

You can have a look the documentation for more information




回答2:


I prefer to use react-native-global-props: https://www.npmjs.com/package/react-native-global-props Very simple: Install, Import and Declare.

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

Declare it in your highest order component.

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

setCustomView(customViewProps);

Now it's for all View in your app.



来源:https://stackoverflow.com/questions/63159757/change-the-default-light-grey-background-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!