javascript - 如何在 React Native 中创建六边形 View

我想使用六边形作为背景图标的组件。 我正在使用从这个网址复制/粘贴我的代码: https://codedaily.io/tutorials/22/The-Shapes-of-React-Native

我的代码:

六边形.js

return (
  <View style={styles.hexagon}>
    <View style={styles.hexagonInner} />
    <View style={styles.hexagonBefore} />
    <View style={styles.hexagonAfter} />
  </View>
);

const styles = StyleSheet.create({
  hexagon: {
    width: 100,
    height: 55
  },
  hexagonInner: {
    width: 100,
    height: 55,
    backgroundColor: 'red'
  },
  hexagonAfter: {
    position: 'absolute',
    bottom: -25,
    left: 0,
    width: 0,
    height: 0,
    borderStyle: 'solid',
    borderLeftWidth: 50,
    borderLeftColor: 'transparent',
    borderRightWidth: 50,
    borderRightColor: 'transparent',
    borderTopWidth: 25,
    borderTopColor: 'red'
  },
  hexagonBefore: {
    position: 'absolute',
    top: -25,
    left: 0,
    width: 0,
    height: 0,
    borderStyle: 'solid',
    borderLeftWidth: 50,
    borderLeftColor: 'transparent',
    borderRightWidth: 50,
    borderRightColor: 'transparent',
    borderBottomWidth: 25,
    borderBottomColor: 'red'

  }
});

然后:

  <View style={{width:100,height:100}}>
    <Hexagon />
 </View>

但它返回一个红色矩形。

最佳答案

如果您遇到问题,请不要制作其他组件,同样这样做,这将返回您想要的六边形!

像这样使用

            <View style={styles.hexagon}>
                <View style={styles.hexagonInner}>
                 <View style={styles.hexagonBefore}>
                 </View>
                </View>
                <View style={styles.hexagonAfter}></View>
              </View>

https://stackoverflow.com/questions/54491762/

相关文章:

typescript - 我想在nestjs中实现自定义缓存

ruby-on-rails - 如何将数组作为绑定(bind)变量传递给 Rails/ActiveR

visual-studio-code - 进行文本搜索时如何防止水平滚动?

reactjs - 使用下拉列表自定义 ag-grid-filter 使用 react-redux

webpack - 如何从 CLI 为 Webpack 的 devServer.contentBas

amazon-web-services - 我可以使用 boto 将 "ok_action"添加到现

r - 编辑 stargazer 源代码——保存的编辑不会呈现为 pdf

pandas - 重新采样 TimeSeries 时无法导入名称 'NaT'

php - 如何在登录后自定义重定向

python-3.x - 在用户控制的目录中生成 python 字节码缓存