简体   繁体   English

React Native,更改React Navigation标题样式

[英]React Native, change React Navigation header styling

I'm implementing React Navigation in my React Native app, and I'm wanting to change the background and foreground colors of the header. 我正在我的React Native应用程序中实现React Navigation ,我想要更改标题的背景和前景色。 I have the following: 我有以下内容:

/**
 * 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);

By default the background color of the heading is white, with a black foreground. 默认情况下,标题的背景颜色为白色,前景为黑色。 I've also looked at the documentation for React Navigation but I'm not able to find where it shows how to set the styling. 我还查看了React Navigation的文档,但我无法找到它显示如何设置样式的位置。 Any help? 有帮助吗?

In newer versions of React Navigation you have a flatter settings object, like below: 在较新版本的React Navigation中,您有一个更平坦的设置对象,如下所示:

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

Deprecated answer: 弃用答案:

Per the docs, here , you modify the navigationOptions object. 根据文档,您可以在此处修改navigationOptions对象。 Try something like: 尝试类似的东西:

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

Please don't actually end up using those colors though! 请不要真的最终使用这些颜色!

According to documentation you can use "navigationOptions" style like this. 根据文档,您可以使用“navigationOptions”样式。

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

For more info about navigationOptions you can also read from docs:- 有关navigationOptions的更多信息,您还可以从文档中读取: -

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

Notice! 注意! navigationOptions is differences between Stack Navigation and Drawer Navigation navigationOptionsStack NavigationDrawer Navigation之间的差异
Stack Navigation Solved. 堆栈导航解决了。
But for Drawer Navigation you Can add Your own Header and Make Your Styles with contentComponent Config: 但对于抽屉导航,您可以添加自己的标题并使用contentComponent配置创建样式:
First import { DrawerItems, DrawerNavigation } from 'react-navigation' Then 首先import { DrawerItems, DrawerNavigation } from 'react-navigation'然后

Header Before DrawerItems : DrawerItems之前的DrawerItems

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

DrawerItems之前的标题

Footer After DrawerItems : DrawerItems之后的页脚:

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

Try this code: 试试这段代码:

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

good luck! 祝好运!

Try this working code 试试这个工作代码

static navigationOptions = {
      title: 'Home',
      headerTintColor: '#ffffff',
      headerStyle: {
        backgroundColor: '#2F95D6',
        borderBottomColor: '#ffffff',
        borderBottomWidth: 3,
      },
      headerTitleStyle: {
        fontSize: 18,
      },
  };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM