【问题标题】:Flexbox positioning text inside imageFlexbox 在图像内定位文本
【发布时间】:2017-02-07 02:16:03
【问题描述】:

我正在使用 React Native 并尝试创建特定的布局。

我有一个图像,垂直和水平居中(在视图内),图像顶部的一些文本也垂直和水平居中。文本需要位于图像/背景图像之上,因此我将其放在图像标签内。

现在该文本的内容可能很长,因为它在图像标签内,所以它会换行。

我怎样才能使 Text 中的内容不会换行并且仍然在 Image 之上?

到目前为止我的布局:

我想要实现的布局

我的代码:

<TouchableHighlight onPress={onPress}>
  <View style={styles.categoryContainer}>
    <View style={styles.leftContainer}>
      <View style={styles.categoryIndexContainer}>
        <Text style={styles.categoryIndex}>01</Text>
      </View>
    </View>
    <View style={styles.middleContainer}>
      <Image source={img} style={styles.categoryImage}>
        <Text style={styles.categoryName}>Some very long title name</Text>
      </Image>
    </View>
    <View style={styles.rightContainer}></View>
  </View>
</TouchableHighlight>

const styles = StyleSheet.create({
  categoryContainer: {
    flexDirection: 'row',
  },
  leftContainer: {
    width: 50,
    paddingLeft: 15,
    paddingTop: 30,
  },
  middleContainer: {
    alignItems: 'center',
    justifyContent: 'center',
    flex: 1,
    flexDirection: 'row',
  },
  rightContainer: {
    width: 50,
  },
  categoryName: {
    color: '#ffffff',
    fontWeight: 'bold',
    fontSize: 40,
    textAlign: 'center',
    backgroundColor: 'transparent',
    flexWrap: 'nowrap',
  },
  categoryImage: {
    alignItems:'center',
    justifyContent:'center',
    flexWrap: 'nowrap',
  },
})

谢谢

【问题讨论】:

    标签: reactjs react-native flexbox css-position react-native-flexbox


    【解决方案1】:

    您应该指定categoryName 样式:position: 'absolute',然后设置topleftrightbottom 属性,直到将Text 放在Image 上方。

    【讨论】:

    • 嗨,我确实在 categoryName 上尝试过绝对定位,但内容仍在换行。
    • 您应该删除 Image 组件中的 Text 组件。它们应该是两个独立的组件
    • 右 - 从图像内部删除文本并添加绝对位置可防止文本换行,但由于某种原因,图像出现在文本上方(即使文本组件位于图像组件之前)。
    • 文字应该在图片之后
    • 你是对的,我的错。有用。非常感谢您的帮助。
    猜你喜欢
    • 2023-03-13
    • 2010-09-05
    • 2022-11-17
    • 2016-12-08
    • 2023-03-30
    • 2017-02-28
    • 2019-05-30
    • 2020-10-22
    • 2017-07-15
    相关资源
    最近更新 更多