Control touchable area in react-native

前端 未结 2 1571
花落未央
花落未央 2021-02-06 22:09

I have a square TouchableOpacity button, whose graphics is just a small dot icon in the middle, and the rest is transparent background. I find that in many Android

相关标签:
2条回答
  • 2021-02-06 22:51

    You can use the View#hitSlop property to increase the touchable area. This can be useful in scenarios where you know that the increased touch area won't overlap with other touchables.

    A more robust solution is to use the padding style property. The touchable area of the Touchable* components includes the element's padding.

    0 讨论(0)
  • 2021-02-06 22:59

    I am just adding some code for easy referencing:

    <View style={Styles.buttonStyle}>
        <TouchableOpacity onPress={onBtn1Pressed} hitSlop={{top: 20, bottom: 20, left: 50, right: 50}}>
            <Text style={Styles.textStyle}>
               Button text
             </Text>
        </TouchableOpacity>
    </View>
    

    Where the parent button container has the following style. Since the width is 150, I have given left and right as 50 to increase the clickable area.

     buttonStyle:{
        alignItems:'center',
        backgroundColor: '#F92660',
        width:150,
        height:50,
        marginTop:20,
        marginBottom:10,
        marginRight:15,
        padding:5,
      },
    
    0 讨论(0)
提交回复
热议问题