2017-10-07 73 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作爲源圖像時,它在左側和右側或頂部和底部都有一些額外的空間。我需要加上這個額外的空間與左側和頂部屬性的絕對位置 –

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

是的,這是真實的觀點,但我需要爲圖像做到這一點。 –

相關問題