React Native,更改React Navigation头样式

o2g1uqev  于 2023-03-31  发布在  React
关注(0)|答案(6)|浏览(152)

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

wwwo4jvm

wwwo4jvm1#

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

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

不推荐答案:

根据文档,在这里,您可以修改navigationOptions对象。尝试如下操作:

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

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

kninwzqo

kninwzqo2#

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

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

有关navigationOptions的更多信息,您也可以从文档中阅读:-
https://reactnavigation.org/docs/navigators/stack#Screen-Navigation-Options

iszxjhcz

iszxjhcz3#

试试这个工作代码

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

sy5wg1nm4#

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

DrawerItems前表头:

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

DrawerItems后页脚:

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

gev0vcfq

gev0vcfq5#

请尝试以下代码:

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

祝你好运!

mwngjboj

mwngjboj6#

百分之百有效。

<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,
  },
});

相关问题