简体   繁体   中英

How to create an hexagon View in react native

I want to use a hexagon shape as a component for background icons. I'm using copy/paste my codes from this url: https://codedaily.io/tutorials/22/The-Shapes-of-React-Native

在此处输入图片说明

my code:

Hexagon.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'

  }
});

then:

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

but It returns a red rectangle.

Don't Make Other Component if you are facing the issue do it on same, this will return your desired hexagon!

Use Like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM