React Native, 改变 React Navigation 标题栏样式

27
我正在我的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);

默认情况下,标题的背景颜色是白色,前景色是黑色。我也查看了React Navigation的文档,但找不到设置样式的地方。有什么帮助吗?

6个回答

43

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

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

已弃用的回答:

根据文档,这里,您需要修改navigationOptions对象。尝试像这样做:

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

请不要真的使用那些颜色!


1
没问题,我昨天才做过这个,这也是我能帮忙的唯一原因。我不得不仔细查阅文档才找到它,很高兴这不是徒劳无功。如果你在ReactNavigation中遇到太多问题,可以尝试查看ReactRouter的4.0版本,它具有本地绑定功能。 - cssko
1
我相信这种“嵌套”的标题方式已经被弃用了 - 请参见下面@VishalDhaduk的答案。 - Freewalker

16

4

尝试使用这个可行的代码

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

我们能添加副标题吗?如果标题是“主页”,我想在下面添加“我的主页”。我该怎么做? - grooot
const CustomHeader = ({ title, subtitle }) => ( <View style={{flex: 1, flexDirection: 'column'}}> <Text>{title}</Text> <Text>{subtitle}</Text> </View> );static navigationOptions = { title: <CustomHeader title="标题" subtitle="副标题"/>, }; - omprakash8080
好的,这会将标题和副标题放在中心右侧,对吗?标题在中心是可以的,在底部我想让副标题在左侧。我该怎么做?在文本之间添加一个视图吗? - grooot
是的,您可以根据最终结果更新CustomHeader布局。 - omprakash8080

2
注意! navigationOptionsStack NavigationDrawer Navigation 之间存在差异。
已解决 Stack Navigation 问题。
但是对于 Drawer Navigation,您可以添加自己的标题并使用 contentComponent 配置创建自己的样式:
首先,import { DrawerItems, DrawerNavigation } from 'react-navigation'。然后:

DrawerItems 前添加标题:

contentComponent: props => <ScrollView><Text>在此添加您自己的标题区域</Text><DrawerItems {...props} /></ScrollView>

在 DrawerItems 之前添加标题

DrawerItems 后添加页脚:

contentComponent: props => <ScrollView><DrawerItems {...props} /><Text>在此添加您自己的页脚区域</Text></ScrollView>


2

尝试使用以下代码:

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

祝你好运!


这不是为标题设置样式,而是将颜色设置为标题、踩。 - Eduardo in Norway

0

它是100%有效的。

   <Stack.Navigator
      initialRouteName="Home"
      screenOptions={{
        headerStyle: styles.header,
        headerTintColor: '#fff',
        headerTitleStyle: styles.headerTitle,
      }}
    >
      <Stack.Screen name="Home" component={HomeScreen} />
    </Stack.Navigator>



const styles = StyleSheet.create({
  header: {
    backgroundColor: '#3f51b5',
  },
  headerTitle: {
    fontWeight: 'bold',
    fontSize: 20,
  },
});

headerStyle: This property allows you to customize the style of the header. You can use it to set the background color, add a border, or add any other style you want.
headerTintColor: This property allows you to set the color of the header text and icons. 
headerTitleStyle: This property allows you to customize the style of the header title. You can use it to set the font size, font family, or any other style you want.

请添加一些解释,说明您在上述代码中使用的内容来解决问题。 - Archit Gargi
@ArchitGargi编辑了帖子以进行解释。 - Mohd. Anas Siddiqui

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接