reactjs - 怎么做标签的行为类似于 react-native 中的 HTML 内联元素
问题描述
我有这个简单的 UI 结构:
<View style={{ flexDirection: 'row', flexWrap: 'wrap' }}>
<Text>A simple fee equal to</Text>
<View style={{ width: 80 }}>
<TextInput style={{ borderWidth: 1, borderColor: 'black' }} />
</View>
<Text>of any settlement or judgment</Text>
</View>
在一个小屏幕上,它会显示这样的东西
如上图所示,我预计文本 ,of any settlement or judgment
不会移动到下一行。
我能想到的最接近的解决方案是将每个文本分成单独的Text
元素。
<View style={{ flexDirection: 'row', flexWrap: 'wrap' }}>
<Text>A simple fee equal to</Text>
<View style={{ width: 80 }}>
<TextInput style={{ borderWidth: 1, borderColor: 'black' }} />
</View>
{'of any settlement or judgment'.split(' ').map((chunk, i) => (
<Text key={i.toString()}>{chunk} </Text>
)}
</View>
结果是这样的:
问题:有没有更好的方法来实现我想要的?或者这是解决问题的唯一方法?
解决方案
好吧,您可以将其写入具有 flexDirection:'row' 样式属性的视图中,也可以将其包装在另一个 Text 中,如下所示:
<Text>
<Text>A simple fee equal to</Text>
<View style={{ width: 80 }}>
<TextInput style={{ borderWidth: 1, borderColor: 'black' }} />
</View>
<Text>Of any settlement or Judgement</Text>
</Text>
推荐阅读
- php - 非英语不支持来自 $.post jquery codeigniter 的 mysql 查询
- html - 在 td 标签中使用 `display:block` 从顶部开始文本?我需要将文本移到底部
- javascript - 当我转换它们时,会出现 div 左侧的一条线
- vba - 单击复选框时如何隐藏文本
- .htaccess - 使用 .htaccess 隐藏网址
- python - 在 pyqt5 中绘制 Matplotlib 图
- deap - 如何编写适用于数据框列的自定义函数作为 DEAP 的原语
- php - Laravel:在查询参数中再次获取 URL 参数
- c - 可变参数 x86-64 ABI。寄存器中浮点参数的数量
- mysql - Laradock 丢失数据 (mysql)