在React Native中,如何将视图放在另一个视图的顶部,其中一部分位于视图的边界之外?

fat*_*oku 52 react-native

我正在尝试使用React Native制作如下所示的布局.

在此输入图像描述

如何指定B相对于A的位置?

使用iOS Interface Builder和autoconstraints,这可以非常明确地完成,并且轻而易举.使用React Native可以实现这一目标并不是那么明显.

bor*_*mes 67

将以下内容添加到"浮动"视图中

position: 'absolute'
Run Code Online (Sandbox Code Playgroud)

您可能还需要添加顶部和左侧的定位值

看到这个例子:https://rnplay.org/apps/OjzcxQ/edit

  • Darn,那个链接已经死了. (50认同)
  • 同样重要的是,"视图B"必须在层次结构中的"视图A"之后,因为现在没有"zIndex"支持. (7认同)
  • 如何在不同的显示尺寸和宽高比下工作? (2认同)

onm*_*133 12

你可以使用这个OverlayContainer。诀窍是absolute100%大小一起使用。检查下面的示例:

// @flow

import React from 'react'
import { View, StyleSheet } from 'react-native'

type Props = {
  behind: React.Component,
  front: React.Component,
  under: React.Component
}

// Show something on top of other
export default class OverlayContainer extends React.Component<Props> {
  render() {
    const { behind, front, under } = this.props

    return (
      <View style={styles.container}>
        <View style={styles.center}>
          <View style={styles.behind}>
            {behind}
          </View>
          {front}
        </View>
        {under}
      </View>
    )
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    height: '100%',
    justifyContent: 'center',
  },
  center: {
    width: '100%',
    height: '100%',
    alignItems: 'center',
    justifyContent: 'center',
  },
  behind: {
    alignItems: 'center',
    justifyContent: 'center',
    position: 'absolute',
    left: 0,
    top: 0,
    width: '100%',
    height: '100%'
  }
})
Run Code Online (Sandbox Code Playgroud)

  • 您确定您的意思是“height: 100,”而不是“height: '100%',”? (2认同)

Abd*_*han 11

上述解决方案不适用于我。我通过创建与父级具有相同背景色的View来解决此问题,并添加了负边距使图像向上移动。

<ScrollView style={{ backgroundColor: 'blue' }}>
  <View
    style={{
      width: '95%',
      paddingLeft: '5%',
      marginTop: 80,
      height: 800,
    }}>
    <View style={{ backgroundColor: 'white' }}>

      <Thumbnail square large source={{uri: uri}} style={{ marginTop: -30 }}/>
      <Text>Some Text</Text>
    </View>
  </View>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)

我得到了以下结果。

在此处输入图片说明


Avn*_*wal 9

import React, {Component} from 'react';
import {StyleSheet, View} from 'react-native';


export default class App extends Component {
  render() {
    return (
       <View>// you need to wrap the two Views an another View
          <View style={styles.box1}></View>
          <View style={styles.box2}></View>
       </View> 
    );
  }
}

const styles = StyleSheet.create({
  box1:{
    height:100,
    width:100,
    backgroundColor:'red'
  },
  box2:{
    height:100,
    width:100,
    backgroundColor:'green',
    position: 'absolute',
    top:10,
    left:30

  },
});
Run Code Online (Sandbox Code Playgroud)


Tha*_*i C 8

您可以使用zIndex将视图放在另一个视图之上.它的工作方式类似于CSS z-index属性 - 具有较大组件的组件zIndex将在顶部呈现.

你可以参考:布局道具

片段:

    <ScrollView>
          <StatusBar backgroundColor="black" barStyle="light-content" />
          <Image style={styles.headerImage} source={{ uri: "http://www.artwallpaperhi.com/thumbnails/detail/20140814/cityscapes%20buildings%20hong%20kong_www.artwallpaperhi.com_18.jpg" }}>
            <View style={styles.back}>
              <TouchableOpacity>
                <Icons name="arrow-back" size={25} color="#ffffff" />
              </TouchableOpacity>
            </View>
            <Image style={styles.subHeaderImage} borderRadius={55} source={{ uri: "https://upload.wikimedia.org/wikipedia/commons/thumb/1/14/Albert_Einstein_1947.jpg/220px-Albert_Einstein_1947.jpg" }} />
          </Image>

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: "white"
    },
    headerImage: {
        height: height(150),
        width: deviceWidth
    },
    subHeaderImage: {
        height: 110,
        width: 110,
        marginTop: height(35),
        marginLeft: width(25),
        borderColor: "white",
        borderWidth: 2,
        zIndex: 5
    },
Run Code Online (Sandbox Code Playgroud)