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

时间:2018-05-31 08:14:17

标签: react-native react-navigation styling

我试图在我的反应原生应用中改变背景的颜色,从灰色到白色。我在渲染后使用react-nav来制作TabNavigator。我试图将这个TabNavigator放在视图中并设置backgroundColor,但所有屏幕都变为白色。我该如何解决这个问题?

index.js

...
render() {
    return (
        <View style={{ backgroundColor: '#FFFFFF'}}>
            <Tabs />
        </View>
    )
  }
...

标签

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

主屏幕

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

5 个答案:

答案 0 :(得分:13)

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

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

答案 1 :(得分:4)

由于弃用,以下内容将不再起作用。

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

您现在必须使用defaultNavigationOptions(请参见下文)。

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

答案 2 :(得分:4)

对于React Navigation 5及更高版本

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

对于React Navigation 4和更早版本

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

答案 3 :(得分:2)

在视图中设置要设置背景颜色的位置

view: {
  backgroundColor: '#FFFFFF' 
}

答案 4 :(得分:0)

像这样编辑您的View标记,

  <View style={{flex: 1,backgroundColor: '#6ED4C8'}}></View>