Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center a view inside another view in React Native?

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: non-centered-cirlces

I want to have blue and red circles concentric. How do I achieve that?

like image 813
Milan Cermak Avatar asked Dec 21 '16 14:12

Milan Cermak


People also ask

How do I center a view in another view?

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.

How do you place a view on top of another view React Native?

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.


1 Answers

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'   }, 
like image 85
agenthunt Avatar answered Sep 20 '22 09:09

agenthunt