代码之家  ›  专栏  ›  技术社区  ›  Joris416

React Native:ZIndex与动态视图

  •  2
  • Joris416  · 技术社区  · 6 年前

    我对React Native很陌生,我尝试构建一个应用程序,在其中我向我的用户展示平铺概述。我用一个包含3列的平面列表构建了这个平铺概览。我需要让它看起来像所有的项目都挂在一根绳子上,绳子在我拥有的瓷砖后面垂直延伸,我试图通过将我的“项目”视图的Z索引设置为1000,将我的“绳子”视图的zIndex设置为1来实现这一点。请参见下面的示例:

    enter image description here

    我手机的代码如下所示:

    export default class ItemButton extends Component {
    
      //
    
    render() {
        const { item, style, onPress } = this.props
        console.log('style for itemButton: ', style)
        return (
            <TouchableOpacity style={style} onPress={onPress}>
                <Image style={{zIndex:1000, width: style.width, height: style.height}} resizeMode={'contain'} source={require('../Artwork/Items/cta-enter.png')}></Image>
                <Image style={{top: -Math.abs(style.height + 100), height: 200, width: 30, zIndex:1, alignSelf: 'center'}} source={require('../Artwork/Items/rope.png')} resizeMode={'contain'}></Image>
            </TouchableOpacity>        
        );
      }
    }
    

    renderCollectionItem(item) {
        return <ItemButton item={item} style={styles.buttonStyle} onPress={() => this.collectionItemPressed(item)} />
      }
    
    render() {
    return (
       <FlatList
      style={{width: collectionViewWidth, flexDirection: 'row', overflow: 'visible', top: this.props.style.top}}
      contentContainerStyle={[{left: this.props.horizontalSpacing / 2}, internalStyles.collectionViewContentStyle]}
      data = { this.convertToIndexed(this.props.items) }
      renderItem = {({item}) => (
        this.renderCollectionItem(item[0])
      )}
      numColumns={(3)}
      key={(Helpers.isPortrait() ? this.state.portraitRowSize : this.state.landscapeRowSize)}
      keyExtractor={(item, index) => index.toString()}
    />
    );
    }
    

    但是,这不起作用,因为React Native始终放置稍后以更高的Z索引渲染的视图。

    问题: 我应该如何配置我的平面列表以获得上图所示的结果?

    1 回复  |  直到 6 年前
        1
  •  -1
  •   khairy Mohamed    6 年前

    不幸的是,react native不支持z索引。。 我在使用动态组件时也遇到了同样的问题, 解决方案是在两个元素的样式中都使用立面属性,这里有一个技巧:

    • 将要位于顶部更高标高的图元设置为15。
    • 将另一个图元设置为另一个图元的底部,例如5。 结果正是你想要的。。