css - 在原生卡中启用溢出

标签 css react-native native-base

我在网站中有一个如下所示的组件:

Regular card

它基本上是一个内部有图像的 div,但图像的 margin-top 为 -50,因此它会溢出卡片。

我想用 react-native 和 native-base 完成同样的事情。相关代码如下:

    render() {
       return (
          <View style={styles.container}>
            <Card>
                <CardItem style={{ marginTop: -50, justifyContent: 'center' }}>
                    <Image style={styles.image} source={{ uri: this.state.band.imageComponent.link }} />
                </CardItem>
            </Card>
         </View>
       )
    }

const styles = StyleSheet.create({
   container: {
    flex: 1,
    marginTop: 150
   },
   image: {
    width: 150,
    height: 150,
   )

结果是这样的: React-native card

如您所见,图片在顶部被截断。我怎样才能让图像溢出并让它像我的第一张图像一样覆盖在卡片上?

最佳答案

Android 不支持溢出。有很多 Unresolved 问题。检查其中一些 herehere .

这是一个 npm package这显然解决了您可以尝试的问题。

否则,您可以使用我在此 medium post 上找到的解决方法.

根据您的图像,您必须将图像和容器包装在另一个 View 中作为 sibling ,然后绝对定位它们。

这是我从 post 中稍微调整过的代码.您可以根据您的 CardCardItem 样式替换 View

<View style={styles.container}>
  <View style={styles.cardSection1}>
    <Image style={styles.image} source={{ uri: 'https://imgplaceholder.com/420x320/ff7f7f/333333/fa-image' }} />
  </View>
  <View style={styles.cardSection2}>
  </View>
</View>

const styles = {
  container: {
   flex: 1,
   backgroundColor: 'grey',
   alignItems: 'center'
  },
  image: {
   width: 150,
   height: 150,
  },
  cardSection1: {
    alignItems: 'center',
    justifyContent: 'center',
    position: 'absolute',
    width: 100,
    height: 100,
    borderRadius: 50 / 2,
    zIndex: 2,
    shadowColor: '#000',
    shadowOffset: { width: 0, height: 0 },
    shadowOpacity: 0.2,
    shadowRadius: 10,
    elevation: 7,
  },
  cardSection2: {
    alignItems: 'center',
    justifyContent: 'center',
    position: 'absolute',
    top: 25,
    width: 300,
    height: 150,
    borderRadius: 8,
    backgroundColor: 'white',
    zIndex: 1,
    shadowColor: '#000',
    shadowOffset: { width: 0, height: 0 },
    shadowOpacity: 0.2,
    shadowRadius: 10,
    elevation: 5,
  }
}

这是我得到的输出。

enter image description here

关于css - 在原生卡中启用溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51682587/

相关文章:

reactjs - 使用 native-base 即时更改主题

reactjs - NativeBase 和 Shoutem 之间,哪个最适合用于 React Native?

javascript - jQuery 克隆几个 "li"并附加到 ul 的特定位置(不是它的末尾)

asp.net - asp :ListBox 的 IE 7 中的最小宽度

css - 使用 Compass 函数而不使用 Compass 来编译样式表

google-maps - React Native 打开原生 map

ios - React Native 没有启动 iOS 模拟器

html - block 元素周围的神秘空白

android - 如何在屏幕之间共享 React Native 组件

javascript - 如何设置默认日期