我试图在React Native中的一张卡的顶部显示一张卡,并将顶部卡设置为zIndex = 1,底部卡设置为zIndex = 0,然后将它们放置在如下视图中:

<View style={{paddingVertical: MARGIN_1, justifyContent: 'center'}}>
  {this.props.subscribed ? (
    <CardSubscribe
      style={{zIndex: 2}}
    />
  ) : (null)}
  {this._renderTextItems()}
</View> 

然而,两张牌之间没有重叠.最下面的卡片在最上面的卡片之后开始:

enter image description here

我还try 过使用inspector进行调试,结果显示最上面的卡的属性为zIndex = 1:

enter image description here

最下面的卡片的属性为zIndex = 0:

enter image description here

我也试着把最上面的牌放在最下面的牌后面,但它只是显示在最下面的牌下面,就像zIndex没有做任何事情一样.我该怎么解决这个问题?

推荐答案

React Native中,我们默认有position: relative个.如果您想正确使用zIndex,您可能需要添加position: 'absolute',以便它们按照用例中的描述重叠.

例如:

<View style={{flex: 1}}>
               <View style={{width: 200, height: 200, zIndex: 1, position: 'absolute', backgroundColor: 'red'}} />
               <View style={{width: 200, height: 200, zIndex: 2, position: 'absolute', backgroundColor: 'blue'}} />
 </View>

React-native相关问答推荐

在 React Native 中打开后日期 Select 器不关闭

根据搜索字符串中的每个字母过滤数组

react-native-snap-carousel 无法正确渲染

如何获取 React Native 应用程序的崩溃日志(log)?

'React native run android' 在模拟器中启动应用程序后立即停止

react-native 中 android 和 iOS 的图像大小

如何以编程方式在 react-native 中截屏

如何渲染多行文本组件,行间有白色间隙

React Navigation TabNavigator:在选项卡更改时重置上一个选项卡

使用 React Native 打开 iOS iPad 模拟器

在关闭 react-native 时更改 Switch 的 colored颜色

使用条码 scanner 掩码的透明视图react-native相机

React Native Expo StackNavigator 重叠通知栏

通过 ToolbarAndroid => onIconClicked 显示 DrawerLayoutAndroid

在Android上更改高度时react-native TextInput显示错误

使用带有样式组件的动画

设置 react-native 元素时使用特定的包名称

React Native:使用百分比时将视图的宽度设置为等于其高度

react-native:多色文本视图

React Native:并排对齐两个 TextInput