在React Native中更改应用程序背景颜色

gno*_*me. 5 styling react-native react-navigation

我正在尝试在我的反应原生应用程序中更改背景的颜色,从灰色到白色.我在渲染后使用react-navigation来制作TabNavigator.我试图将这个TabNavigator放在视图中并设置backgroundColor,但所有屏幕都变为白色.我怎么解决这个问题?

index.js

...
render() {
    return (
        <View style={{ backgroundColor: '#FFFFFF'}}>
            <Tabs />
        </View>
    )
  }
...
Run Code Online (Sandbox Code Playgroud)

标签

...
const Tabs = TabNavigator(
  {
    Home: {
      screen: HomeStack,
      navigationOptions: {
        title: 'Acasa',
      },
    },
    ...
    Account: {
      screen: AccountScreen,
      navigationOptions: {
        title: 'Contul meu',
      },
    },
  },
  {
    tabBarComponent: props => <FooterNavigation {...props} />,
    tabBarPosition: 'bottom',
    initialRouteName: 'Home',
  },
);
...
Run Code Online (Sandbox Code Playgroud)

主屏幕

render() {
    const {
      data, refreshing, loading, error,
    } = this.state;

    return (
      <ScrollView>
        <Container>
          {error && <Text>Error</Text>}
          {loading && <ActivityIndicator animating size="large" />}

          <List>
            <FlatList
              data={data}
              renderItem={({ item }) => (
                <ListItem onPress={() => this.props.navigation.navigate('Item', item)}>
                  <Item data={item} />
                </ListItem>
              )}
              // ID from database as a key
              keyExtractor={item => item.title}
              ItemSeparatorComponent={this.renderSeparator}
              ListFooterComponent={this.renderFooter}
              ListHeaderComponent={this.renderHeader}
              refreshing={refreshing}
              onRefresh={this.handleRefresh}
              onEndReached={this.handleLoadMore}
              onEndReachedThreshold={0}
            />
          </List>
        </Container>
      </ScrollView>
    );
  }
Run Code Online (Sandbox Code Playgroud)

Moh*_*SOU 30

对于 React 导航 6,

<Stack.Navigator  screenOptions={{
     contentStyle:{
       backgroundColor:'#FFFFFF'
     }
  }}  initialRouteName="Home">
Run Code Online (Sandbox Code Playgroud)


Ter*_*ius 20

对于 React Navigation 5 及更高版本

<Stack.Navigator
    initialRouteName='dashboard'
    screenOptions={{
        headerStyle: { elevation: 0 },
        cardStyle: { backgroundColor: '#fff' }
    }}
>
    <Stack.Screen name="Home" component={HomeStack} />
</Stack.Navigator>
Run Code Online (Sandbox Code Playgroud)

对于 React Navigation 4 及更早版本

const HomeStack = StackNavigator(
{
    Home: {
        screen: HomeScreen,
    },
},
{
    headerMode: 'screen',
    cardStyle: { backgroundColor: '#fff' },
},
);
Run Code Online (Sandbox Code Playgroud)


gno*_*me. 19

我已经解决了我的问题,它是由StackNavigator引起的.要解决它,只需添加一些额外的选项

    const HomeStack = StackNavigator(
  {
    Home: {
      screen: HomeScreen,
    },
    Item: {
      screen: ItemScreen,
      navigationOptions: ({ navigation }) => ({
        title: `${navigation.state.params.title}`,
      }),
    },
  },
  **
  {
    headerMode: 'screen',
    cardStyle: { backgroundColor: '#FFFFFF' },
  },
  **
);
Run Code Online (Sandbox Code Playgroud)


lak*_*ara 8

像这样编辑您的 View 标签,

  <View style={{flex: 1,backgroundColor: '#6ED4C8'}}></View>
Run Code Online (Sandbox Code Playgroud)


Ome*_*lan 5

要设置的正确道具是sceneContainerStyle

  <BottomTab.Navigator
    ...
    sceneContainerStyle={{ backgroundColor: 'white' }}
  >
      ...
   </BottomTab.Navigator>
Run Code Online (Sandbox Code Playgroud)


She*_*mir 5

对于 React 导航 6 -->

堆栈导航器:

<Stack.Navigator 
    screenOptions={{ contentStyle: {backgroundColor: 'white'} }}
>
Run Code Online (Sandbox Code Playgroud)

选项卡导航器:

<Tab.Navigator
    sceneContainerStyle={{backgroundColor: 'white'}}
    ...
>
Run Code Online (Sandbox Code Playgroud)