RN中如何在View标签中生成一竖排圆圈
可以使用RN的StyleSheet和View组件来实现。具体步骤如下:
- 创建一个样式表,定义圆圈的样式:
const styles = StyleSheet.create({
circle: {
width: 20,
height: 20,
borderRadius: 10,
backgroundColor: 'red',
marginVertical: 5,
},
});
- 在View组件中使用循环语句生成一组圆圈:
<View>
{[...Array(5)].map((_, i) => (
<View key={i} style={styles.circle} />
))}
</View>
其中,[...Array(5)] 表示生成一个长度为5的数组,map 方法遍历这个数组,生成5个圆圈。
完整代码如下:
import React from 'react';
import { View, StyleSheet } from 'react-native';
const styles = StyleSheet.create({
circle: {
width: 20,
height: 20,
borderRadius: 10,
backgroundColor: 'red',
marginVertical: 5,
},
});
export default function App() {
return (
<View>
{[...Array(5)].map((_, i) => (
<View key={i} style={styles.circle} />
))}
</View>
);
}
运行效果如下:
![vertical-circles](https://i.imgur.com/CGw6TgH.png
原文地址: http://www.cveoy.top/t/topic/hpSU 著作权归作者所有。请勿转载和采集!