2017-10-07 2 views

답변

0

정확하게 position: 'absolute'topleft 값을 사용하여 올바르게 이미지 스타일을 지정해야한다고 생각합니다. 아래는 그 예입니다.

참고 : 이미지가 네트워크에있는 경우 (미리 크기를 모를 경우) 이미지를 인라인으로 스타일을 지정할 수 있습니다. style={{ width: img.width, height: img.height }} 이미지 크기 (React Native Retrieve Actual Image Sizes)

를 가져 오는 후
import { Dimensions, StyleSheet, View, Image } from 'react-native'; 

const { width, height } = Dimensions.get('window'); 

const styles = StyleSheet.create({ 
    container: { 
    flex: 1 
    }, 
    background: { 
    width, 
    height 
    }, 
    blue: { 
    position: 'absolute', 
    top: 10, 
    left: 10, 
    width: 200, 
    height: 200 
    }, 
    green: { 
    position: 'absolute', 
    top: 100, 
    left: 200, 
    width: 200, 
    height: 200 
    }, 
    red: { 
    position: 'absolute', 
    top: 400, 
    left: 150, 
    width: 200, 
    height: 200 
    } 
}); 

const Demo =() => (
    <View style={styles.container}> 
    <Image 
     style={styles.background} 
     source={{ uri: 'http://via.placeholder.com/1080x1920' }} 
    /> 
    <Image 
     style={styles.blue} 
     source={{ uri: 'http://via.placeholder.com/200/0000ff' }} 
    /> 
    <Image 
     style={styles.green} 
     source={{ uri: 'http://via.placeholder.com/200/008000' }} 
    /> 
    <Image 
     style={styles.red} 
     source={{ uri: 'http://via.placeholder.com/200/ff0000' }} 
    /> 
    </View> 
); 

export default Demo; 

결과 :result

+0

감사합니다. 올바른 가로 세로 비율로 원본 이미지를 렌더링해야합니다. 소스 이미지에 resizeMode를 사용하면 왼쪽 및 오른쪽 또는 위쪽과 아래쪽에 여유 공간이 생깁니다. 그리고 절대 위치에서 left와 top 속성을 가진이 여분의 공간을 더해야합니다. –

0

당신은 첫 번째 이미지의 상단에 표시됩니다 다른 3 개 이미지의 절대 위치를 사용할 수 있습니다

확인 아래 코드 :

export default class AbsoluteImages extends Component { 
    constructor(props) { 
    super(props); 
    } 

    render() { 
    return (
     <View style={{ flex:1 }}> 
     <View style={{backgroundColor:"red" , flex:1}}> 
     </View> 
     <View style={{backgroundColor:"blue" , position:"absolute" , left:20 , top:50 , width:100,height:100}}> 
     </View> 
     <View style={{backgroundColor:"yellow" , position:"absolute" , left:120 , top:160 , width:100,height:100}}> 
     </View> 
     <View style={{backgroundColor:"green" , position:"absolute" , left:50 , top:300 , width:100,height:100}}> 
     </View> 
     </View> 
    ); 
    } 
} 
+0

예,보기에는 사실이지만 이미지에는이를 수행해야합니다. –

관련 문제