【问题标题】:React Native, change React Navigation header stylingReact Native,更改 React Navigation 标题样式
【发布时间】:2017-08-08 21:37:54
【问题描述】:

我在我的 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 的文档,但我无法找到它显示如何设置样式的位置。有什么帮助吗?

【问题讨论】:

    标签: javascript reactjs react-native react-navigation


    【解决方案1】:

    试试这个代码:

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

    祝你好运!

    【讨论】:

    • 这不是设置标题样式,而是将颜色设置为标题,否决
    【解决方案2】:

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

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

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

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

    【讨论】:

      【解决方案3】:

      试试这个工作代码

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

      【讨论】:

      • 可以加字幕吗???如果标题是“家”,我想在它下面添加“我的家”。我该怎么做??
      • 是的,您也可以添加字幕。我正在为它添加样本。 const CustomHeader = ({ title, subtitle }) => ( {title}{subtitle}查看> ); static navigationOptions = { title: , };
      • 好的,这会将标题和副标题放在中间对吗?中间的标题没问题??在底部我希望字幕在左边。我怎么做?在文本之间添加视图?
      • 是的,您可以根据最终结果更新 CustomHeader 布局。
      【解决方案4】:

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

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

      不推荐使用的答案:

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

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

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

      【讨论】:

      • 没问题,我昨天才这样做的事实是我可以提供帮助的唯一原因。我不得不搜索文档才能找到它。我很高兴这一切都不是徒劳的。如果您对 ReactNavigation 有太多问题,请查看 ReactRouter 的 4.0 分支,它有原生绑定。
      • 我相信这种“嵌套”的标题方式已被弃用 - 请参阅下面@VishalDhaduk 的回答。
      【解决方案5】:

      注意! navigationOptionsStack NavigationDrawer Navigation 之间的区别
      堆栈导航已解决。
      但是对于抽屉导航,您可以添加自己的标题并使用 contentComponent 配置创建样式:
      import { DrawerItems, DrawerNavigation } from 'react-navigation' 然后

      DrawerItems之前的标题:

      contentComponent: props =&gt; &lt;ScrollView&gt;&lt;Text&gt;Your Own Header Area Before&lt;/Text&gt;&lt;DrawerItems {...props} /&gt;&lt;/ScrollView&gt;.

      DrawerItems 之后的页脚:

      contentComponent: props =&gt; &lt;ScrollView&gt;&lt;DrawerItems {...props} /&gt;&lt;Text&gt;Your Own Footer Area After&lt;/Text&gt;&lt;/ScrollView&gt;.

      【讨论】:

        猜你喜欢
        • 2021-01-06
        • 1970-01-01
        • 1970-01-01
        • 2018-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-10
        相关资源
        最近更新 更多