react-native - 在 React Native 中将绝对位置 View 带到前面

标签 react-native z-index react-native-stylesheet

Android 上出现这种情况,iOS 上运行正常。我有一个带有徽章的组件,也是我定制的。

function HomeMenuCell({ title, imagePath, pressEvent, showBadge, badgeText }) {
    return (
        <TouchableOpacity style={styles.container} onPress={pressEvent}>
            <View style={styles.imageView}>
                <Image source={imagePath} />
            </View>
            <Text style={styles.titleText}>{title}</Text>
            {showBadge && <View style={styles.badgeView}>
                <Text style={styles.badgeText}>{badgeText}</Text>
            </View>}
        </TouchableOpacity>
    );
}

我有一个 TouchableOpacity 组件,在这个容器中,我正在做所有事情。 badgeView 也是基于 container

定位的
    container: {
        justifyContent: 'center',
        alignItems: 'center',
        marginBottom: 10,
        marginTop: 10,
        marginRight: 10,
        marginLeft: 20
    },
    badgeView: {
        position: 'absolute',
        backgroundColor: 'pink',
        borderRadius: 8,
        padding: 3,
        // zInndex: 999, //This doesn't work
        right: 0,
        top: 0
    },

但它隐藏在imageView后面

    imageView: {
        width: 50, height: 50,
        shadowOffset: { width: 0, height: 0 },
        shadowColor: 'black',
        shadowOpacity: 0.2,
        elevation: 3,
        justifyContent: 'center',
        alignItems: 'center',
        borderRadius: 8,
    },

我什至之前尝试过放置这些代码行。我认为这是因为 Prop 没有加载并且 View 被渲染。那么,有什么想法吗?

创建了这个快速小吃(仅在 Android 中可见):https://snack.expo.io/@chtalha/badge-on-view

最佳答案

   badgeView: {
    position: 'absolute',
    backgroundColor: 'red',
    borderRadius: 8,
    padding: 3,
    shadowColor: "#000",
    shadowOffset: {
      width: 0,
      height: 2,
    },
    shadowOpacity: 0.25,
    shadowRadius: 3.84,
    elevation: 5,
    right: -15,
    top: -5
},

在您的徽章 View 中使用此样式希望这对您有用:) 发生这种情况是因为您已经在图像中使用了海拔,因此如果您想在其顶部显示其他 View ,那么您必须设置比 ImageView 更高的海拔

关于react-native - 在 React Native 中将绝对位置 View 带到前面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68453258/

相关文章:

javascript - React-Native:无法访问状态值或从 renderRow 声明的任何方法

animation - 如何在 React Native 中设置 fontWeight 动画

html - css 布局、div 定位、对齐和 z-index 的问题

react-native - React Native - 使用 StyleSheet 与普通对象相比有什么好处?

javascript - 如何在 React Native 的 StyleSheet.create 中继承样式(可能使用解构语法)

react-native - React Native 会截断行中的最后一个单词,但如果不适合则需要删除整个单词

react-native - react 原生 : Task :react-native-maps:compileDebugRenderscript FAILED

node.js - 我的 npm install -g react-native-cli 不起作用

css - Google Maps API V3 Z-index 不工作

css - IE-7 Z-index问题