3.2 Absolute & Relative
1.absolute
class Position extends Component {
render() {
return (
<View style={styles.container}>
<View style={styles.box1}>
<Text style={styles.text}>1</Text>
</View>
<View style={styles.box2}>
<Text style={styles.text}>2</Text>
</View>
<View style={styles.box3}>
<Text style={styles.text}>3</Text>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1
},
box1: {
position: 'absolute',
top: 40,
left: 40,
width: 100,
height: 100,
backgroundColor: 'red'
},
box2: {
position: 'absolute',
top: 80,
left: 80,
width: 100,
height: 100,
backgroundColor: 'blue'
},
box3: {
position: 'absolute',
top: 120,
left: 120,
width: 100,
height: 100,
backgroundColor: 'green'
},
text: {
color: '#ffffff',
fontSize: 80
}
});
box2: {
position: 'absolute',
top: 80,
left: 80,
width: 100,
height: 100,
backgroundColor: 'blue',
transform: [{'translate': [0,0, 1]}]
},
3.relative(default)
class Relative extends Component {
render() {
return (
<View style={styles.container}>
<View style={styles.box1}>
<Text style={styles.text}>1</Text>
<View style={styles.ball}/>
</View>
<View style={styles.box2}>
<Text style={styles.text}>2</Text>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1
},
box1: {
position: 'relative',
top: 40,
left: 40,
width: 100,
height: 100,
backgroundColor: 'red'
},
box2: {
position: 'absolute',
top: 100,
left: 100,
width: 100,
height: 100,
backgroundColor: 'blue'
},
ball: {
position: 'absolute',
top: 40,
left: 40,
width: 40,
height: 40,
borderRadius: 20,
backgroundColor: 'yellow'
},
text: {
color: '#ffffff',
fontSize: 80
}
});
4.fixed
class Fixed extends Component {
render() {
return (
<View style={styles.container}>
<View style={styles.tbar}>
<Text style={styles.text}>Fixed top bar</Text>
</View>
<ScrollView style={styles.main}>
<View style={styles.item}><Text style={styles.text}>1</Text></View>
<View style={styles.item}><Text style={styles.text}>2</Text></View>
<View style={styles.item}><Text style={styles.text}>3</Text></View>
</ScrollView>
<View style={styles.bbar}>
<Text style={styles.text}>Fixed bottom bar</Text>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1
},
tbar: {
width: 375,
height: 100,
borderBottomWidth: 5,
borderColor: 'black',
backgroundColor: 'red'
},
main: {
flex: 1
},
item: {
height: 200,
width: 375,
marginTop: 10,
backgroundColor: 'green'
},
bbar: {
width: 375,
height: 100,
borderTopWidth: 5,
borderColor: 'black',
backgroundColor: 'red'
},
text: {
color: '#ffffff',
fontSize: 40
}
});
原文: https://unbug.gitbooks.io/react-native-training/content/32absolute&_relative.html