【问题标题】:Enable overflow in native-base cards在本机基础卡中启用溢出
【发布时间】:2018-08-04 05:00:49
【问题描述】:

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

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

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

【问题讨论】:

    标签: css react-native native-base


    【解决方案1】:

    Android 不支持溢出。有很多未解决的问题。检查其中一些herehere

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

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

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

    这是我从 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,
      }
    }
    

    这是我得到的输出。

    【讨论】:

    • 很高兴我能帮上忙
    【解决方案2】:

    //import liraries
    import React, { Component } from 'react';
    import { View, Text, StyleSheet,TextInput,Dimensions,Image } from 'react-native';
    import ButtonRoundBorder from '../components/ButtonRoundBorder';
    
    
    const window = Dimensions.get('window')
    // create a component
    class Login extends Component {
        render() {
            return (
                <View style={styles.container}>
                    <Image style={{width:window.width,height:window.height,position:'absolute'}} source={require('../images/bg2.jpeg')}/>
                    <View  style={styles.overlayImageStyle} >
                        <Image style={{flex:1,borderRadius:80}} resizeMode={'contain'} source={require('../images/profile.jpg')} />
                    </View>
                    <View   style={styles.cardStyle}>
                        <View style={styles.insideCard}>
                            <Text style={{fontWeight:'bold',fontSize:20,alignSelf:'center'}}>Login</Text>
                            <TextInput style={[styles.textInputStyle,{marginTop:30}]} underlineColorAndroid='#000000' placeholder='Enter Email' />
                            <TextInput style={[styles.textInputStyle,{marginTop:20}]} underlineColorAndroid='#000000'  placeholder='Enter Password' />
                            <ButtonRoundBorder style={{marginTop:40}} title='Login'/>
                        </View>
                    </View>
                </View>
                
            );
        }   
    }                               
    
    
    const styles = StyleSheet.create({ 
    
        container:{
            flex: 1,
            alignItems: 'center',
            backgroundColor: 'transparent',
        },
        overlayImageStyle:{
            alignItems: 'center',
            justifyContent: 'center',
            position: 'absolute',
            width: 100,
            height: 100,
            borderRadius: 100/2,
            backgroundColor: 'white',
            top:50,
            shadowColor: '#000', 
            // position: 'absolute',row
            // shadowOpacity: 0.2, 
            // shadowRadius: 10,
            elevation: 7,
        },
        cardStyle:{
            // alignItems: 'center',
            // justifyContent: 'center',
            // position: 'absolute',
            top: 80,
            width: window.width - 40,
            height: window.height - 200,
            borderRadius: 8,
            backgroundColor: 'white',
            // backgroundColor: '#7E57C2',
            shadowColor: '#000',
            elevation: 5,
        },
        insideCard:{
            top: 80,
            alignContent: 'center',
            justifyContent: 'center',   
            flexDirection:'column',
            
        },
        textInputStyle:{
            width:300,
            height:40,
            alignSelf:'center',
            
        }
    
    });
    
    //make this component available to the app
    export default Login;

    here is the link for screenshot

    【讨论】:

    • 请。你能解释为什么这是答案吗?
    • 我创建了 2 个视图,并使用 position: ' absolute' 使一个与另一个重叠。使用这种方法是因为它比使用原生基础更容易,而我在尝试使用原生基础时失败了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-19
    • 2022-08-07
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多