Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native Android Negative Margins

I'm working on a React Native app that has a user avatar component with an overlap effect:

enter image description here

I was able to get it working on iOS since it allows negative margins, but when you use negative margins on Android, it clips the last image like this:

enter image description here

Here are the styles I'm using:

avatarContainer: {
    borderRadius: 20,
    width: 40,
    height: 40,
    marginRight: -11
},

avatar: {
    borderRadius: 20,
    width: 40,
    height: 40
},

avatarContainer is the white circle behind the image and avatar is the image itself.

What is the best approach that works on both platforms to accomplish the desired styling?

like image 991
Zach Avatar asked Oct 25 '16 19:10

Zach


People also ask

Can we give negative margin Android?

Negative margin is not supported on Android. Use absolute layout.

How do you use overflow in react native?

According to the article, you can use react-native-view'overflow library (a bridging header written to support the overflow in react-native android. All you need to do is wrap the overflowcomponent in the <ViewOverflow> . Hope this helps! Save this answer.

How do I change the line height in react native?

React Native provide lineHeight style props to make vertical space between text. lineHeight default value is 0 use if developer not defined lineHeight for text.


1 Answers

I've tried with pretty much your setup + flexbox and everything seems to work ok.

enter image description here enter image description here

class App extends React.Component {
  render() {
    const { overlapContainer, avatarContainer, avatar} = styles;

    return (
        <View style={overlapContainer}>

          <View style={avatarContainer}>
            <Image style={avatar} source={{ uri: 'http://lorempixel.com/output/cats-q-c-100-100-3.jpg' }} />
          </View>

          <View style={avatarContainer}>
            <Image style={avatar} source={{ uri: 'http://lorempixel.com/output/cats-q-c-100-100-7.jpg' }} />
          </View>

          <View style={avatarContainer}>
            <Image style={avatar} source={{ uri: 'http://lorempixel.com/output/cats-q-c-100-100-3.jpg' }} />
          </View>

          <View style={avatarContainer}>
            <Image style={avatar} source={{ uri: 'http://lorempixel.com/output/cats-q-c-100-100-7.jpg' }} />
          </View>

        </View>
    );
  }
}


const styles = {
  overlapContainer: {
    flexDirection: 'row-reverse',
    justifyContent: 'flex-end',
    marginTop: 50,
    marginRight: 50
  },
  avatarContainer: {
    borderRadius: 33,
    height: 66,
    width: 66,
    marginLeft: -15,
    borderStyle: 'solid',
    borderWidth: 3,
    borderColor: 'white'
  },
  avatar: {
    borderRadius: 30,
    height: 60,
    width: 60
  }
}
like image 68
Piotr Berebecki Avatar answered Sep 23 '22 01:09

Piotr Berebecki