我试图在视图底部添加阴影,但我不知道如何在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(预期)

右:安卓

编辑:我发现的唯一"假"解决方案是使用react-native-linear-gradient来创建自定义阴影.

IOS/ANDROID

推荐答案

You can use overflow: 'hidden' to achieve the desired result without having to install a library.
wrap the view in a parent view and set the parent's overflow to hidden and apply a padding only on the side where you want your shadow to appear like so:

  <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>

结果:result

here's a custom component that you can use:

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;

example:

<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>

你可以根据你的阴影来调整填充

React-native相关问答推荐

expo 使用错误的版本代码创建建筑

单击时更改ToucheableOpacity colored颜色 不起作用

如何根据设备主题更改React Native中状态栏的背景?

在 React Native 中,水平滚动不适用于我的整个应用程序

在 react native 平面列表中动态定位弹出窗口

我应该什么时候调用 realm.close()?

在 TouchableOpacity 上创建 Raised凸起或Shadow阴影效果

React Native Android:how to remove "overscroll effect" of ScrollView

加载作为props传递的图像

React Native TextInput 没有获得焦点

React-Native:save user preferences

如何使用功能组件向 ref 公开功能?

在特定屏幕上使用react-navigation修改后退按钮

如何将捕获的图像与 react-native-camera 一起使用

React Native Navigation 组件路由问题

KeyboardAvoidingView - 隐藏键盘时重置高度

如何在 React Native 中将图像放置在其他图像之上?

将 async 和 await 与 export const 一起使用

如何从react-native应用程序打开外部应用程序?

React Native + Redux 基本认证