如何渲染阴影?

use*_*846 55 javascript react-native

如何在视图上渲染阴影?我尝试过shadowColor,shadowOffset,shadowOpacity和shadowRadius的许多组合,它们似乎什么都不做.我确信样式正确应用,因为我设置的其他属性工作.

Ash*_*k R 83

我正在使用React-Native 0.40以下代码适用于我.

(需要提升才能在其上投下阴影)

 class MainApp extends Component {
  render() {
    return (
      <View style={styles.container}>

        <View elevation={5} style={styles.buttonContainer}>
          <Text style={styles.textStyle}>Shadow Applied</Text>
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#FFFFFF'
  },
  textStyle: {
    color: '#FFFFFF'
  },
  buttonContainer: {
    backgroundColor: '#2E9298',
    borderRadius: 10,
    padding: 10,
    shadowColor: '#000000',
    shadowOffset: {
      width: 0,
      height: 3
    },
    shadowRadius: 5,
    shadowOpacity: 1.0
  }
})
Run Code Online (Sandbox Code Playgroud)

在iPhone上测试过.

在此输入图像描述

  • 对于那些在android上,backgroundColor是至关重要的.我使用View作为另一个元素的容器,在指定背景颜色之前无法获得阴影. (6认同)

Kar*_*mar 29

使用高程在RN Android上实现阴影.增加了提升#27

<View elevation={5}> </View>

  • 它不支持Android版本<5.0 (3认同)
  • 现在您可以将其设置为样式http://facebook.github.io/react-native/releases/0.26/docs/view.html#style (2认同)

wan*_*gii 14

它似乎是React native中的一个错误,它shadowOpacity被设置为类型CGFloat而不是float根据CALayer doc.在修复之前使用iPhone 5模拟器.(CGFloatfloat在旧设备).

跟踪此问题的React Native问题是:

https://github.com/facebook/react-native/issues/449


Vik*_*kii 12

    viewStyle : {
    backgroundColor: '#F8F8F8',
    justifyContent: 'center',
    alignItems: 'center',
    height: 60,
    paddingTop: 15,
    shadowColor: '#000',
    shadowOffset: { width: 0, height: 2 },
    shadowOpacity: 0.2,
    marginBottom: 10,
    elevation: 2,
    position: 'relative'
},
Run Code Online (Sandbox Code Playgroud)

使用marginBottom:10


Sau*_*shi 11

你必须给View提升高度

<View elevation={5} style={styles.container}>
   <Text>Hello World !</Text>
 </View>
Run Code Online (Sandbox Code Playgroud)

样式可以像这样添加:

 const styles = StyleSheet.create({

     container:{
        padding:20,
        backgroundColor:'#d9d9d9',
        shadowColor: "#000000",
        shadowOpacity: 0.8,
        shadowRadius: 2,
        shadowOffset: {
          height: 1,
          width: 1
        }
       },
   })
Run Code Online (Sandbox Code Playgroud)


小智 8

  panel: {
    // ios
    backgroundColor: '#03A9F4',
    alignItems: 'center', 
    shadowOffset: {width: 0, height: 13}, 
    shadowOpacity: 0.3,
    shadowRadius: 6,

    // android (Android +5.0)
    elevation: 3,
  }
Run Code Online (Sandbox Code Playgroud)

或者您可以使用react-native-shadow for android


小智 6

所有关于利润

这适用于Android,但没有在ios中测试它

import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import { View, Platform } from 'react-native'
import EStyleSheet from 'react-native-extended-stylesheet'

const styles = EStyleSheet.create({
    wrapper: {
        margin: '-1.4rem'
    },
    shadow: {
        padding: '1.4rem',
        margin: '1.4rem',
        borderRadius: 4,
        borderWidth: 0,
        borderColor: 'transparent',
        ...Platform.select({
            ios: {
                shadowColor: 'rgba(0,0,0, 0.4)',
                shadowOffset: { height: 1, width: 1 },
                shadowOpacity: 0.7,
                shadowRadius: '1.4rem'
            },
            android: {
                elevation: '1.4rem'
            }
        })
    },
    container: {
        padding: 10,
        margin: '-1.4rem',
        borderRadius: 4,
        borderWidth: 0,
        borderColor: '#Fff',
        backgroundColor: '#fff'
    }
})

class ShadowWrapper extends PureComponent {
    static propTypes = {
        children: PropTypes.oneOfType([
            PropTypes.element,
            PropTypes.node,
            PropTypes.arrayOf(PropTypes.element)
        ]).isRequired
    }

    render () {
        return (
            View style={styles.wrapper}
                View style={styles.shadow}
                    View style={styles.container}
                        {this.props.children}
                    View
                View
            View
        )
    }
}

export default ShadowWrapper
Run Code Online (Sandbox Code Playgroud)


小智 6

按样式组件

const StyledView = styled.View`
      border-width: 1;
      border-radius: 2;
      border-color: #ddd;
      border-bottom-width: 0;
      shadow-color: #000;
      shadow-offset: {width: 0, height: 2};
      shadow-opacity: 0.8;
      shadow-radius: 2;
      elevation: 1;     
`
Run Code Online (Sandbox Code Playgroud)

或按风格

const styles = StyleSheet.create({
  containerStyle: {
    borderWidth: 1,
    borderRadius: 2,
    borderColor: '#ddd',
    borderBottomWidth: 0,
    shadowColor: '#000',
    shadowOffset: { width: 0, height: 2 },
    shadowOpacity: 0.8,
    shadowRadius: 2,
    elevation: 1,
    marginLeft: 5,
    marginRight: 5,
    marginTop: 10,
  }
})
Run Code Online (Sandbox Code Playgroud)