Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-native <View> component's "elevation" style is causing ugly shadows

The elevation style attribute enables box-shadows for Android 5.0+.

Am I doing something unusual with 'elevation' here to cause the ugliness that can be seen in the screenshot below? Also, is there any way to define a shadow-offset?

The emulator is running 6.0 (> 5.0), so that's not the problem. I'm running react-native 25.1.

  "dependencies": {
    "react": "^0.14.8",
    "react-native": "^0.25.1",
    "react-native-gcm-android": "^0.2.0",
    "react-native-material-design": "^0.3.5",
    "react-native-system-notification": "^0.1.10",
    "react-redux": "^4.4.5",
    "redux": "^3.5.2"
  }

Here's react-native's documentation for View component styling

Here's my render method:

  render() {
    return (
      <ListView
        dataSource={alertData}
        renderRow={(rowData) =>
          <View style={style.cardContainer}>
            <Text>{rowData.blah}</Text>
            <Text>{"#" + rowData.foo}</Text>
            <Text>{rowData.blah}</Text>
            <Text>{rowData.foo}</Text>
            <Text>{rowData.baz}</Text>
          </View>
        }
      />
    );
  }

And the style declaration:

var style = StyleSheet.create({
  cardContainer : {
    elevation   : 3,
    flex        : 1,
    margin      : 10,
    padding     : 10,
    borderWidth : 2,
    borderColor : beeStyles.colors.lightGray
  }
});

Which somehow it results in this ...

enter image description here

like image 431
Squeaky Avatar asked Jun 16 '16 20:06

Squeaky


People also ask

How do I change the elevation shadow color in React Native?

Unfortunately, the color of the shadow produced by Android's setElevation cannot be changed. Also, their Material Design guidelines around the web like here don't seem to indicate that you can change the color. In addition, box shadows in React Native are also not supported on Android, based on this. Save this answer.

How do you provide shadow to view in React Native?

On Android, we need to use the elevation view style prop from react-native to add shadows. elevation: Sets the elevation of a view, using Android's underlying elevation API. This adds a drop shadow to the item and affects z-order for overlapping views.

How do you show shadow only on bottom in React Native?

To set elevation shadow only on the bottom on React Native, we wrap out View with another View and set its overflow to 'hidden' . to set overflow to 'hidden' on the outer View . And then we add the shadow styles in the inner view to add the shadow.

How do you give shadow to image in React Native?

Wrap your Image inside View (for semantic clarity) and then define following style rules to the View: shadow: { shadowColor: '#202020', shadowOffset: {width: 0, height: 0}, shadowRadius: 5, }, I made an example here: https://snack.expo.io/rJesdOgRZ.


1 Answers

The missing piece is backgroundColor. Adding backgroundColor : '<anything>' style to the View container makes those weird inner shadows disappear.

like image 141
Squeaky Avatar answered Nov 26 '22 06:11

Squeaky