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

标签 javascript react-native

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

enter image description here

我的代码:

六边形.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>

关于javascript - 如何在 React Native 中创建六边形 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54491762/

相关文章:

javascript - 我想检查用户是否在 React 应用程序中在线?

javascript - Heroku ExecJS::ProgramError: Unexpected token: error rails

javascript - 参数缺失 && ||括号中的运算符但仍在工作

Javascript - 如何在文本字符串中设置特定单词的样式?

android - react native : Implementing Reward Referral (Invite and Earn)

android - 打开应用程序时不会出现工具栏,但如果我将设备转为横向,则会出现

javascript - Google Oauth 没有获得刷新 token

android - npm WARN jest-react-native@17.0.0 需要 whatwg-fetch@^1.0.0 的对等体,但没有安装

javascript - 不变违规 : Element type is invalid: React Native

android - gradlew assembleRelease 和 gradlew assembleReleaseStaging 生成 APK 无需更改