【问题标题】:Why do my in-line styles work, but my stylesheet isn't changing the code at all?为什么我的内联样式有效,但我的样式表根本没有更改代码?
【发布时间】:2019-08-26 10:23:56
【问题描述】:

我正在用 React Native 编写导航栏。可以将其视为双层——上层是汉堡菜单、标题和搜索图标,第二层由三个可触摸的标题组成,用于导航到相关屏幕。

a mock-up of the navbar I'm trying to create

当我应用内联样式时,它们会起作用。当我在那里执行 Stylesheet.create 并应用样式时,它们不会。我是编程新手,很困惑。

我的计划是编写一个单独的导航栏,该导航栏分为两行(视图):navbarTop 和 navbarBottom。我非常感谢您了解这样做是否有意义,以及如何解决我的样式问题。非常感谢大家!

import React, { Component } from 'react';
import { View, Text, StyleSheet, Image, TouchableHighlight } from 'react-native';
import { withNavigation } from 'react-navigation';

class Navbar extends Component {
  render() {
    return (
      <View style={styles.navbarTop}>
        <View>
          <TouchableHighlight
            onPress={() => { 
              this.props.navigation.openDrawer();
            }}
          >
            <Image
              style={{marginBottom: 5}}
              source={require('./../../../android/app/src/main/res/drawable/menu.png')}
            />
          </TouchableHighlight>
        </View>

        <View>
          <Text
            style={{
              fontWeight: 'bold',
              color: 'white',
              fontSize: 20,
              marginRight: 70
            }}
          >
            Dashboard
          </Text>
        </View>

        <View>
          <Image
            style={{marginBottom: 5}}
            source={require('./../../../android/app/src/main/res/drawable/search.png')}
          />  
        </View>

        <View style={styles.navbarBottom}>
          <View>
            <Text
              style={{
                color: 'white',
                fontSize: 15,
                marginRight: 70
              }}
            > RECORDINGS </Text>
          </View>

          <View>
            <Text
              style={{
                color: 'white',
                fontSize: 15,
                marginRight: 70
              }}
            > PATIENTS </Text>
          </View>

          <View>
            <Text
              style={{
                color: 'white',
                fontSize: 15,
                marginRight: 70
              }}
            > DEVICES </Text>
          </View>
        </View>
      </View>
    );
  }
}

export default withNavigation(Navbar);

const styles = StyleSheet.create({
  navbarTop: {
    backgroundColor: '#14172B',
    padding: 10,
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'flex-end',
  },
  // navbarBottom: {

  // }
});

【问题讨论】:

  • 嗨@dereknahman,在 CSS 中,内联样式比内部和外部 CSS 具有更高的优先级。这就是为什么你的内联 CSS 只在这里工作。尝试检查其他 CSS 是否对您的 CSS 产生影响。

标签: css react-native react-native-android


【解决方案1】:

在线条样式中覆盖样式表中存在的样式。

假设有一个样式组件:

<View style = {[styles.demoView,{marginTop:50}]}/>


export default StyleSheet.create({
  demoView: {
    marginTop:20,
    flexDirection: "row",
    padding: "10rem",
    justifyContent: "space-between",
    alignItems: "center",
  },

在上述样式中,内联样式将替换 marginTop 的值,因为它具有高优先级,即使您从样式表中删除 marginTop 属性,它也不会受到内联样式的抑制。希望它能给你清晰的视野。 快乐编码:)

编辑:

    import React, { Component } from 'react';
import { View, Text, StyleSheet, Image, TouchableHighlight } from 'react-native';
import { withNavigation } from 'react-navigation';

class Navbar extends Component {
  render() {
    return (
      <View style={styles.navbarTop}>
          <TouchableHighlight 
            onPress={() => { this.props.navigation.openDrawer()}}>
            <Image style={{marginBottom: 5}}
            source={require('./../../../android/app/src/main/res/drawable/menu.png')}
            />
          </TouchableHighlight>

          <Text style={{fontWeight: 'bold',color: 'white',fontSize: 20,marginRight: 70}}>
            Dashboard
          </Text>

          <Image
            style={{marginBottom: 5}}
            source={require('./../../../android/app/src/main/res/drawable/search.png')}
          />  

        <View style={styles.navbarBottom}>
            <Text style={styles.navBarText}> RECORDINGS </Text>
<Text style={styles.navBarText}> PATIENTS </Text>
<Text style={styles.navBarText}> DEVICES </Text>
          </View>
        </View>
    );
  }
}

export default withNavigation(Navbar);

const styles = StyleSheet.create({
  navbarTop: {
    backgroundColor: '#14172B',
    padding: 10,
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'flex-end',
  },
  // navbarBottom: {

  // }
  navBarText:{
      color: 'white',
      fontSize: 15,
      marginRight: 70
      }
});

【讨论】:

  • 谢谢!你有什么技巧可以让我的应用听 StyleSheet.create 而不是只听内联样式吗?
  • 答案已经在我发布的答案中。如果样式是内联的,则不会从样式表调用该特定值,而是从内联样式中获取,因为它们具有高优先级。
  • 如果答案有帮助,请接受,以便其他面临同样疑问的人有更清晰的视野
  • 但是 StyleSheet.create 中的样式即使完全删除内联样式也不会影响我的应用程序的外观,因此内联样式优先于 StyleSheet.create 似乎不是问题在这种情况下
  • 您可能做错了什么。分享完整的代码。
猜你喜欢
  • 2014-05-03
  • 2011-08-02
  • 2018-12-19
  • 1970-01-01
  • 1970-01-01
  • 2012-09-27
  • 1970-01-01
  • 2020-03-05
  • 1970-01-01
相关资源
最近更新 更多