React Native,更改React Navigation标题样式

15 javascript reactjs react-native react-navigation

我正在我的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);
Run Code Online (Sandbox Code Playgroud)

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

css*_*sko 28

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

static navigationOptions = {
  title: 'Chat',
  headerStyle: { backgroundColor: 'red' },
  headerTitleStyle: { color: 'green' },
}
Run Code Online (Sandbox Code Playgroud)

弃用答案:

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

static navigationOptions = {
    title: 'Welcome',
    header: {
        style: {{ backgroundColor: 'red' }},
        titleStyle: {{ color: 'green' }},
    }
}
Run Code Online (Sandbox Code Playgroud)

请不要真的最终使用这些颜色!


Vis*_*duk 15

根据文档,您可以使用"navigationOptions"样式.

static navigationOptions = {
  title: 'Chat',
  headerStyle:{ backgroundColor: '#FFF'},
  headerTitleStyle:{ color: 'green'},
}
Run Code Online (Sandbox Code Playgroud)

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

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

  • 这是1.0.0-beta.11的正确方法 (2认同)