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

时间:2018-11-19 19:39:32

标签: android react-native

我不知道如何使我的组件可触摸。我有一个包装,其位置为:relative,两个元素的位置为:absolute。我想使此包装器可触摸,但不适用于react-native的任何Touchable组件。

此backgroundColor仅用于测试。

<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"
 }

2 个答案:

答案 0 :(得分:0)

您需要为 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>

答案 1 :(得分:-1)

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

相关问题