24

我正在我的 React Native 应用程序中实现React Navigation,并且我想更改标题的背景和前景色。我有以下内容:

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View
} from 'react-native';
import { StackNavigator } from 'react-navigation';

export default class ReactNativePlayground extends Component {

  static navigationOptions = {
    title: 'Welcome',
  };

  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to React Native!
        </Text>
        <Text style={styles.instructions}>
          To get started, edit index.android.js
        </Text>
        <Text style={styles.instructions}>
          Double tap R on your keyboard to reload,{'\n'}
          Shake or press menu button for dev menu
        </Text>
      </View>
    );
  }

}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

const SimpleApp = StackNavigator({
  Home: { screen: ReactNativePlayground }
});

AppRegistry.registerComponent('ReactNativePlayground', () => SimpleApp);

默认情况下,标题的背景颜色为白色,前景为黑色。我还查看了 React Navigation 的文档,但我无法找到它显示如何设置样式的位置。有什么帮助吗?

4

5 回答 5

41

在较新版本的 React Navigation 中,您有一个更扁平的设置对象,如下所示:

static navigationOptions = {
  title: 'Chat',
  headerStyle: { backgroundColor: 'red' },
  headerTitleStyle: { color: 'green' },
}

不推荐使用的答案:

根据文档,您可以在此处修改 navigationOptions 对象。尝试类似:

static navigationOptions = {
    title: 'Welcome',
    header: {
        style: {{ backgroundColor: 'red' }},
        titleStyle: {{ color: 'green' }},
    }
}

请不要实际上最终使用这些颜色!

于 2017-03-17T11:15:36.163 回答
15

根据文档,您可以像这样使用“navigationOptions”样式。

static navigationOptions = {
  title: 'Chat',
  headerStyle:{ backgroundColor: '#FFF'},
  headerTitleStyle:{ color: 'green'},
}

有关 navigationOptions 的更多信息,您还可以从文档中阅读:-

https://reactnavigation.org/docs/navigators/stack#Screen-Navigation-Options

于 2017-05-05T08:33:25.480 回答
4

试试这个工作代码

static navigationOptions = {
      title: 'Home',
      headerTintColor: '#ffffff',
      headerStyle: {
        backgroundColor: '#2F95D6',
        borderBottomColor: '#ffffff',
        borderBottomWidth: 3,
      },
      headerTitleStyle: {
        fontSize: 18,
      },
  };
于 2018-01-18T12:34:32.670 回答
2

注意!是和 Stack Navigation SolvednavigationOptions之间的区别。 但是对于抽屉导航,您可以添加自己的标题并使用配置制作样式: 首先然后Stack NavigationDrawer Navigation

contentComponent
import { DrawerItems, DrawerNavigation } from 'react-navigation'

标题之前DrawerItems

contentComponent: props => <ScrollView><Text>Your Own Header Area Before</Text><DrawerItems {...props} /></ScrollView>.

DrawerItems 之前的标题

页脚之后DrawerItems

contentComponent: props => <ScrollView><DrawerItems {...props} /><Text>Your Own Footer Area After</Text></ScrollView>.

于 2017-07-17T09:56:00.313 回答
1

试试这个代码:

static navigationOptions = {
    headerTitle: 'SignIn',
    headerTintColor: '#F44336'
};

祝你好运!

于 2017-05-07T09:03:58.227 回答