【问题标题】:React Native Image front of other viewReact Native Image 前面的其他视图
【发布时间】:2017-09-06 03:43:08
【问题描述】:

我想编写样式表来显示如下图所示的布局:

头像:

我这样写代码:

<View style={styles.header}></View>
<View style={styles.meInfor}>
    <Image style={styles.avatar}
      source={require('../../images/cristiano-ronaldo.jpg')} />
</View>

和风格:

header:{
        width: width,
        height: 100,
        backgroundColor: '#1270BA',
    },
    meInfor:{
        width: width,
        backgroundColor: '#FFFFFF',
        alignItems: 'center',
        justifyContent: 'center',
    },
    avatar: {
        marginTop: -50,
        width: 100,
        height: 100,
        borderRadius: 100,
    }

我使用marginTop: - 50将头像拉到顶部,但结果如下:

不好的结果:

我怎样才能让这个头像的样式像第一张图片一样?

【问题讨论】:

  • 尝试在头像中使用 position:'absolute'
  • 我添加了这个属性并隐藏了头像。 :(
  • 同时删除边距
  • 仍然不适合我
  • 等我试试看

标签: css react-native


【解决方案1】:

试试这段代码,它在我的情况下运行良好

组件

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


export default class App extends Component {
  render() {
  const {header, meInfor, avatar, container} = styles; 
    return (
      <View style={container}>
        <View style={header}></View>
        <View style={meInfor}></View>
        <Image style={avatar}
      source={{uri: 'http://infosahabat.com/wp-content/uploads/2015/12/Permainan-Bola-Besar.jpg'}} 
       />
      </View>
    );
  }
}

这就是风格

风格

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: 'lightgray',
    },
    header:{
        height: 100,
        backgroundColor: '#1270BA',
    },
    meInfor:{
        backgroundColor: '#FFF',
        top: 0,
        height: 50,
        width: (Dimensions.get('window').width),   
    },
    avatar: {
        position: 'absolute',
        top: 50,
        left: (Dimensions.get('window').width / 2) - 50,
        alignItems: 'center',
        width: 100,
        height: 100,
        borderColor: 'black',
        borderWidth: 1,
        borderRadius: 100,
    }
});

【讨论】:

  • 我想要 meInfor 宽度 = Dimensions.get('window').width。并且只有头像上的borderRadius曲线,而不是正方形。
  • 我已经更新了我的代码repl.it/KkQ9/5 试试看是不是你想要的
【解决方案2】:

像这样改变你的样式表

header : {
      width: width,
      height: 100,
      backgroundColor: '#1270BA'
    },
    meInfor : {
      position: 'absolute',

      width: width,

      alignItems: 'center',
      justifyContent: 'center'
    },
    avatar : {
      marginTop: 50,
      width: 100,
      height: 100,
      borderRadius: 100
    }

【讨论】:

  • 谢谢你,但不适合我。我通过两个轴图像来修正结果。
  • 您是否在父视图中包裹了两个视图?
  • 是的,它们被容器视图包裹:container: { flex: 1, alignItems: 'center', justifyContent: 'flex-start', },
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-18
  • 1970-01-01
  • 1970-01-01
  • 2019-07-07
  • 2020-09-25
  • 1970-01-01
相关资源
最近更新 更多