如何仅在 React Native 的底部设置高程阴影?
How can I set elevation shadow only on the bottom on react native?
我想在视图底部添加阴影,但我不知道如何在 Android 上添加。使用 elevation
它也会在顶部放置一个阴影。有没有办法像 iOS 那样做?
这是我的代码:
export function makeElevation(elevation) {
const iosShadowElevation = {
shadowOpacity: 0.0015 * elevation + 0.18,
shadowRadius: 0.5 * elevation,
shadowOffset: {
height: 0.6 * elevation,
},
};
const androidShadowElevation = {
elevation,
};
return Platform.OS === 'ios' ? iosShadowElevation : androidShadowElevation;
}
左:iOS(预期)
右:Android
编辑:我发现的唯一“假”解决方案是使用 react-native-linear-gradient
来创建自定义阴影。
遗憾的是RN默认不支持,检查打击link
https://ethercreative.github.io/react-native-shadow-generator/
但是你可以使用像 this
这样的 npm 包
您可以使用 overflow: 'hidden'
实现所需的结果,而无需安装库。
将视图包裹在父视图中并将父视图的溢出设置为隐藏,并仅在您希望阴影出现的一侧应用填充,如下所示:
<View style={{ overflow: 'hidden', paddingBottom: 5 }}>
<View
style={{
backgroundColor: '#fff',
width: 300,
height: 60,
shadowColor: '#000',
shadowOffset: { width: 1, height: 1 },
shadowOpacity: 0.4,
shadowRadius: 3,
elevation: 5,
}}
/>
</View>
结果:
这是您可以使用的自定义组件:
import React from 'react'
import { View, StyleSheet, ViewPropTypes } from 'react-native'
import PropTypes from 'prop-types'
const SingleSidedShadowBox = ({ children, style }) => (
<View style={[ styles.container, style ]}>
{ children }
</View>
);
const styles = StyleSheet.create({
container:{
overflow: 'hidden',
paddingBottom: 5,
}
});
SingleSidedShadowBox.propTypes = {
children: PropTypes.element,
style: ViewPropTypes.style,
};
export default SingleSidedShadowBox;
示例:
<SingleSidedShadowBox style={{width: '90%', height: 40}}>
<View style={{
backgroundColor: '#fff',
width: '100%',
height: '100%',
shadowColor: '#000',
shadowOffset: { width: 1, height: 1 },
shadowOpacity: 0.4,
shadowRadius: 3,
elevation: 5,
}} />
</SingleSidedShadowBox>
你可以根据你的阴影调整内边距
上面的答案很有帮助,但对我来说最有效的解决方法是将两者都包装起来,1-删除阴影的组件和 2-阴影被放置在具有样式规则 overflow: "hidden"
的组件中 的组件
示例:
function SomeScreen (){
return (
<View style={{overflow: "hidden"}}/*Top shadow is hidden*/>
<NavBar style={styles.shadow}/*The component dropping a shadow*/ />
<ProductList /*The component under the shadow*/ />
</View>
);
}
const styles = StyleSheet.create({
shadow: {
shadowColor: "black",
shadowOffset: { width: 0, height: 4 },
shadowRadius: 6,
shadowOpacity: 0.2,
elevation: 3,
}
});
只是 append/put 这个到主视图容器:
const styles = StyleSheet.create({
container: {
...Platform.select({
ios: {
shadowColor: '#000',
shadowOffset: {width: 1, height: 3},
shadowOpacity: 0.4,
},
android: {
elevation: 4,
},
}),
},
});
我想在视图底部添加阴影,但我不知道如何在 Android 上添加。使用 elevation
它也会在顶部放置一个阴影。有没有办法像 iOS 那样做?
这是我的代码:
export function makeElevation(elevation) {
const iosShadowElevation = {
shadowOpacity: 0.0015 * elevation + 0.18,
shadowRadius: 0.5 * elevation,
shadowOffset: {
height: 0.6 * elevation,
},
};
const androidShadowElevation = {
elevation,
};
return Platform.OS === 'ios' ? iosShadowElevation : androidShadowElevation;
}
左:iOS(预期)
右:Android
编辑:我发现的唯一“假”解决方案是使用 react-native-linear-gradient
来创建自定义阴影。
遗憾的是RN默认不支持,检查打击link https://ethercreative.github.io/react-native-shadow-generator/
但是你可以使用像 this
这样的 npm 包您可以使用 overflow: 'hidden'
实现所需的结果,而无需安装库。
将视图包裹在父视图中并将父视图的溢出设置为隐藏,并仅在您希望阴影出现的一侧应用填充,如下所示:
<View style={{ overflow: 'hidden', paddingBottom: 5 }}>
<View
style={{
backgroundColor: '#fff',
width: 300,
height: 60,
shadowColor: '#000',
shadowOffset: { width: 1, height: 1 },
shadowOpacity: 0.4,
shadowRadius: 3,
elevation: 5,
}}
/>
</View>
结果:
这是您可以使用的自定义组件:
import React from 'react'
import { View, StyleSheet, ViewPropTypes } from 'react-native'
import PropTypes from 'prop-types'
const SingleSidedShadowBox = ({ children, style }) => (
<View style={[ styles.container, style ]}>
{ children }
</View>
);
const styles = StyleSheet.create({
container:{
overflow: 'hidden',
paddingBottom: 5,
}
});
SingleSidedShadowBox.propTypes = {
children: PropTypes.element,
style: ViewPropTypes.style,
};
export default SingleSidedShadowBox;
示例:
<SingleSidedShadowBox style={{width: '90%', height: 40}}>
<View style={{
backgroundColor: '#fff',
width: '100%',
height: '100%',
shadowColor: '#000',
shadowOffset: { width: 1, height: 1 },
shadowOpacity: 0.4,
shadowRadius: 3,
elevation: 5,
}} />
</SingleSidedShadowBox>
你可以根据你的阴影调整内边距
上面的答案很有帮助,但对我来说最有效的解决方法是将两者都包装起来,1-删除阴影的组件和 2-阴影被放置在具有样式规则 overflow: "hidden"
示例:
function SomeScreen (){
return (
<View style={{overflow: "hidden"}}/*Top shadow is hidden*/>
<NavBar style={styles.shadow}/*The component dropping a shadow*/ />
<ProductList /*The component under the shadow*/ />
</View>
);
}
const styles = StyleSheet.create({
shadow: {
shadowColor: "black",
shadowOffset: { width: 0, height: 4 },
shadowRadius: 6,
shadowOpacity: 0.2,
elevation: 3,
}
});
只是 append/put 这个到主视图容器:
const styles = StyleSheet.create({
container: {
...Platform.select({
ios: {
shadowColor: '#000',
shadowOffset: {width: 1, height: 3},
shadowOpacity: 0.4,
},
android: {
elevation: 4,
},
}),
},
});