可触摸不适用于位置:绝对



我不知道如何让我的组件可触摸。我有一个包装器,它有position:relative和两个position:absolute元素。我想让这个包装器是可触摸的,但它不适用于来自react native的任何可触摸组件。

此背景颜色仅用于测试。

<View style={styles.container}>
<TouchableHighlight onPress={() => { console.log('touched') }}>
<View style={styles.wrapper}>
<View style={styles.wrapperForImage} />
<Text style={styles.text} >{text.toUpperCase()}</Text>
</View>
</TouchableHighlight>
</View>
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center'
},
wrapper: {
position: 'relative',
flex: 1,
alignItems: 'center',
justifyContent: 'center'
},
wrapperForImage: {
position: 'absolute',
width: 200,
height: 200,
backgroundColor: 'red'
},
text: {
position: 'absolute',
width: 100,
height: 100,
zIndex: 10,
textAlign: 'center',
color: '#000',
fontSize: 14,
lineHeight: 23
}

});

依赖项:

"dependencies": {
"expo": "^31.0.0",
"react": "16.5.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-31.0.0.tar.gz",
"react-native-extended-stylesheet": "^0.10.0",
"tslib": "^1.9.3"
}

您需要为TouchableHighlight提供高度宽度

<View style={styles.container}>
<TouchableHighlight style={{
backgroundColor: 'green',
height: '100%',
width: '100%',
}}
onPress={() => { alert('touched') }}>
<View style={styles.wrapper}>
<View style={styles.wrapperForImage} />
<Text style={styles.text} >TEXT</Text>
</View>
</TouchableHighlight>
</View>

我在Android上的解决方案是从父容器中删除elevation样式。

最新更新