自定义形状按钮react-Native

时间:2018-02-25 21:17:41

标签: react-native-android

如何添加自定义形状按钮,例如响应本机应用程序的五边形按钮。任何帮助或意见将不胜感激。

1 个答案:

答案 0 :(得分:0)

以下是实现目标的一种方法(View Snack):

export class PentagonButton extends Component {
  render() {
    return (
        <TouchableOpacity onPress={() => { }}>
          <View style={styles.pentagon}>
            <View style={styles.pentagonInner} />
            <View style={styles.pentagonBefore} />
          </View>
        </TouchableOpacity>
    );
  }
}


const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  pentagon: {
    backgroundColor: 'transparent'
  },
  pentagonInner: {
    width: 90,
    borderBottomColor: 'red',
    borderBottomWidth: 0,
    borderLeftColor: 'transparent',
    borderLeftWidth: 18,
    borderRightColor: 'transparent',
    borderRightWidth: 18,
    borderTopColor: 'red',
    borderTopWidth: 50
  },
  pentagonBefore: {
    position: 'absolute',
    height: 0,
    width: 0,
    top: -35,
    left: 0,
    borderStyle: 'solid',
    borderBottomColor: 'red',
    borderBottomWidth: 35,
    borderLeftColor: 'transparent',
    borderLeftWidth: 45,
    borderRightColor: 'transparent',
    borderRightWidth: 45,
    borderTopWidth: 0,
    borderTopColor: 'transparent',
  }
});

此示例使用不同形状的视图来构建按钮。归功于已@browniefed

lot of different shapes here.

另一种解决方法是使用React ART,它是在React Native中创建形状的好方法。 Here are the unofficial docs.