You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native中flexDirection: row不生效问题求助

问题解决:Text组件始终显示在TouchableOpacity下方,flexDirection: row不生效

问题出在一个极易忽略的拼写错误上:你把内层包裹<TouchableOpacity>和<Text>的<View>的属性写成了styles(复数),正确写法应为style(单数)。这个错误导致itemLeft里设置的flexDirection: 'row'完全没被应用,而View默认采用垂直的column布局,所以Text自然跑到了TouchableOpacity的下方。

修改后的代码如下(仅修正一处拼写错误):

const Task = (props) => {
  return (
    <View style={styles.item}>
      {/* 此处将styles改为style */}
      <View style={styles.itemLeft}>
        <TouchableOpacity style={styles.square}></TouchableOpacity>
        <Text style={styles.itemText}>{props.text}</Text>
      </View>
      <View style={styles.circular}></View>
    </View>
  )
}

const styles = StyleSheet.create({
  item: {
    backgroundColor: '#FFF',
    padding: 15,
    borderRadius: 10,
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'space-between',
    marginBottom: 20,
  },
  itemLeft: {
    flexDirection: 'row',
    alignItems: 'center',
    // flexWrap: 'nowrap',
  },
  square: {
    width: 24,
    height: 24,
    backgroundColor: '#FAA300',
    opacity: 0.4,
    borderRadius: 5, 
    marginRight: 15,
  },
  itemText: {
    maxWidth: '80%',
    // minWidth: '80%',
  },
  circular: {
    width: 12,
    height: 12,
    borderColor: '#55BCF6',
    borderWidth: 2,
    borderRadius: 5,
  },
})

修正后,itemLeft的横向布局会正常生效,TouchableOpacity和Text将横向排列,不会再出现上下堆叠的情况。

内容的提问来源于stack exchange,提问作者rennoib

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 06:36:03