How can I stretch my subview across 100% width of its parent, minus 20px margin on each side? In other words, I need it to fill the width of the parent, with 20px open on each side.
I know in React-Native I can use width: '80%'
to make my subview’s width relative to that of its parent, but then it’s not always precisely 20px on the sides. I also know that I can use alignSelf: 'stretch'
, however that is not working for me – it has unexpected / unreliable results. I don’t want to use Dimensions, as the parent will not always be the device’s screen, so Dimensions.get('window').width
is inadequate for this problem.
What other options do I have?
Advertisement
Answer
Use nested View
. You can try here: https://snack.expo.io/@vasylnahuliak/stackoverflow-67989491
JavaScript
x
29
29
1
import React, { useState } from 'react';
2
import { Text, View, StyleSheet } from 'react-native';
3
4
const App = () => {
5
return (
6
<View style={styles.container}>
7
<View style={styles.child}>
8
<Text style={styles.childText}>child</Text>
9
</View>
10
</View>
11
);
12
};
13
14
const styles = StyleSheet.create({
15
container: {
16
padding: 24,
17
backgroundColor: 'tomato',
18
},
19
child: {
20
height: 150,
21
backgroundColor: 'navy',
22
},
23
childText: {
24
color: 'white',
25
},
26
});
27
28
export default App;
29