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 ...
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.
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.
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.
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.
The missing piece is backgroundColor. Adding backgroundColor : '<anything>'
style to the View container makes those weird inner shadows disappear.
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