如何使 View 元素的所有 Text 子元素具有相同的样式?

Lui*_*izo 7 css text styles view react-native

我想要一个类似于 CSS 的东西,你可以为 div 提供“颜色”样式,并且其中的任何文本都会有它。

我在一个视图组件中有多个文本组件,我希望它们都具有相同的文本颜色。

如果我将“颜色”样式传递给视图,它会发出警告。

当他们的父母可以拥有所有孩子的风格时,我想避免将相同的风格传递给所有这些孩子。

我想从这里开始:

<View>
  <Text style={styles.Warning}>
  </Text>
  <Text style={styles.Warning}>
  </Text>
  <Text style={styles.Warning}>
  </Text>
  <Text style={styles.Warning}>
  </Text>
  <Text style={styles.Warning}>
  </Text>
</View>
Run Code Online (Sandbox Code Playgroud)

对此:

<View style={styles.Warning}>
  <Text>
  </Text>
  <Text>
  </Text>
  <Text>
  </Text>
  <Text>
  </Text>
  <Text>
  </Text>
</View>
Run Code Online (Sandbox Code Playgroud)

样式为:

const styles = StyleSheet.create({
  Warning:{
    color:'#a94442'
  }
});
Run Code Online (Sandbox Code Playgroud)

(如果我不用安装任何东西就更好了)谢谢。

Nee*_*ala 4

是的,您绝对可以做您想做的事情。您唯一缺少的部分是将Text容器放在容器内View

例子:

<View>
 <Text style={{color:'red'}}>
   <Text>
     text1 // will inherit red color
   </Text>
   <Text>
     text2 // will inherit red color
   </Text>
 </Text>
</View>
Run Code Online (Sandbox Code Playgroud)

有关更多信息,您还可以查看链接 https://facebook.github.io/react-native/docs/text.html#containers

  • 但这不是OP想要的布局吗?您现在将“Text”元素嵌套在“Text”元素内。 (11认同)