【发布时间】: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