【问题标题】:React Native: On Android, a button covered by position absolute view can still be tappedReact Native:在Android上,位置绝对视图覆盖的按钮仍然可以点击
【发布时间】:2021-09-12 11:01:30
【问题描述】:

场景: 我有一个红色、绿色和蓝色的盒子,每个盒子里面都有一个可以触摸的黄色盒子。红色和蓝色拳击手是兄弟姐妹。绿色盒子是红色盒子的一个孩子,并且是 position: absolute ,所以它是重叠的。在绿色框(即红色框)的父级上使用 zIndex 我设法将绿色框覆盖在红色和蓝色框上。每个可触摸对象在按下时都会记录其父对象的颜色。

[这是 Android 唯一问题]

问题: 点击可触摸的绿色框,在绿色与蓝色重叠的区域,尽管绿色在顶部,但点击蓝色框而不是绿色框。

预期行为:应该点击绿色框,因为它位于顶部。

截图: https://i.ibb.co/PgBsHmn/android-issue-with-position-absolute.png

小吃: https://snack.expo.io/@bgcodes/absolute-position-issue-on-android

代码:

import { View, TouchableOpacity, StyleSheet, Text } from 'react-native';

const App = () => {
return (
  <View style={styles.container}>
    <View style={styles.red}>
      <TouchableOpacity onPress={()=>console.log('red')}>
        <View style={styles.yellow} />
      </TouchableOpacity>
      <View style={styles.green}>
        <TouchableOpacity onPress={()=>console.log('green')}>
          <View style={styles.yellow} />
        </TouchableOpacity>
      </View>
    </View>
    <View style={styles.blue}>
      <TouchableOpacity onPress={()=>console.log('blue')}>
        <View style={styles.yellow} />
      </TouchableOpacity>
    </View>
  </View>
)}

export default App;

const styles=StyleSheet.create({
  container: {
    justifyContent: 'center',
    alignItems: 'center',
    flex: 1,
  },
  yellow: {
    height: 90,
    width: 90,
    backgroundColor: 'yellow',
  },
  red: {
    justifyContent: 'center',
    alignItems: 'center',
    height: 100,
    width: 100,
    backgroundColor: 'red',
    zIndex: 5,
  },
  green: {
    justifyContent: 'center',
    alignItems: 'center',
    height: 100,
    width: 100,
    backgroundColor: 'green',
    position: 'absolute',
    left: 30,
    top: 50,
  },
  blue: {
    justifyContent: 'center',
    alignItems: 'center',
    height: 100,
    width: 100,
    backgroundColor: 'blue',
    zIndex: 1,
  },
});

任何帮助将不胜感激????

【问题讨论】:

    标签: android react-native z-index absolute


    【解决方案1】:

    渲染对于您的功能来说很棘手。 类似的东西应该可以工作(伪代码)

    现在,您的代码如下所示:

    <RedSquareComponent />
    <GreenSquare />
    <BlueSquare />
    

    为了让绿色方块在蓝色方块上方,只需将两者切换即可。

    <RedSquareComponent />
    <BlueSquare />
    <GreenSquare />
    

    这是零食:

    https://snack.expo.io/@sportelli/absolute-position-issue-on-android

    【讨论】:

    • 让 成为 的子节点对我来说很重要。
    【解决方案2】:

    好的,作为一种解决方法,您可以从“react-native-gesture-handler”导入 TouchableOpacity,它工作正常。 但是使用 TextInput 时问题仍然存在。即,如果在重叠的绿色框下方有一个 TextInput(而不是蓝色框),则按下绿色框将关注 TextInput。

    【讨论】:

      【解决方案3】:

      我觉得应该不错:

      import React from 'react';
      import { View, TouchableOpacity, StyleSheet, Text } from 'react-native';
      
      const App = () => {
        return (
          <View style={styles.container}>
            <View style={styles.red}>
              <TouchableOpacity
                onPress={() => console.log('red')}
                style={styles.yellow}
              />
            </View>
            <View style={styles.green}>
              <TouchableOpacity
                onPress={() => console.log('green')}
                style={styles.yellow}
              />
            </View>
            <View style={styles.blue}>
              <TouchableOpacity
                onPress={() => console.log('blue')}
                style={styles.yellow}
              />
            </View>
          </View>
        );
      };
      
      export default App;
      
      const styles = StyleSheet.create({
        container: {
          justifyContent: 'center',
          alignItems: 'center',
          flex: 1,
        },
        yellow: {
          height: 90,
          width: 90,
          backgroundColor: 'yellow',
        },
        red: {
          justifyContent: 'center',
          alignItems: 'center',
          height: 100,
          width: 100,
          backgroundColor: 'red',
          zIndex: 5,
        },
        green: {
          justifyContent: 'center',
          alignItems: 'center',
          height: 100,
          width: 100,
          backgroundColor: 'green',
          position: 'absolute',
          left: 170,
          zIndex: 6,
        },
        blue: {
          justifyContent: 'center',
          alignItems: 'center',
          height: 100,
          width: 100,
          backgroundColor: 'blue',
          zIndex: 1,
        },
      });
      

      【讨论】:

      • 你在安卓或iOS上测试过吗?我可以在android上重现。
      • 对不起,我忘了说,这是 android 唯一的问题。请在 Android 而不是 ios 或 web 上尝试。
      • 哦,是的,我明白了。我现在尝试了不同的东西,但没有运气。
      • 我认为你必须找到一种方法将你的 3 TouchableOpacity 放在同一个视图中。在这里,您的视图中有黄色和绿色,而另一个有蓝色,我认为这就是您的问题所在。
      • 让 成为 的子节点对我来说很重要。我想要实现的是让一个视图的子视图位于其兄弟视图之上,但它只是在视觉上。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-09
      • 1970-01-01
      • 1970-01-01
      • 2020-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多