I want to center one view inside another one in React Native.
This is my code:
const styles = StyleSheet.create({ container: { flex: 1, flexDirection: 'column', justifyContent: 'center', alignItems: 'center', backgroundColor: 'yellow', }, outerCircle: { backgroundColor: 'blue', width: 100, height: 100, borderRadius: 100/2, }, innerCircle: { backgroundColor: 'red', width: 80, height: 80, borderRadius: 80/2, } }); export default class RecorderButton extends React.Component { _buttonPressAction() { Alert.alert("button pressed"); } render() { return ( <TouchableOpacity activeOpacity={0.4} onPress={this._buttonPressAction} style={styles.container}> <View style={styles.outerCircle}> <View style={styles.innerCircle} /> </View> </TouchableOpacity> ); } }
And this is how it looks:
I want to have blue and red circles concentric. How do I achieve that?
If your views in RelativeLayout follow these steps: 1- wrap your view that wants to be aligned in the center of target view in Framelayout. 2- move all layout attributes to FrameLayout. 3- set layout_align_top and layout_align_bottom (or start and end if horizontal) to target view.
We use the style property position to align one view over another. The position of the view which is beneath the surface should be made relative whereas the position of views above it should be absolute. Make use of properties such as top, left, right and bottom to align the views.
You are already centering in the container. Follow the same for outerCircle as well.
outerCircle: { backgroundColor: 'blue', width: 100, height: 100, borderRadius: 100/2, justifyContent: 'center', alignItems: 'center' },
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With