简体   繁体   English

React Native Elevation 阴影无法正常工作

[英]React Native Elevation shadow does not work properly

I am new to React Native, I tried to create a component for my radio button but I encountered some issue with the Style when I added the elevation, and it is adding the shadow inside the View instead of outside of the View我是 React Native 的新手,我尝试为我的单选按钮创建一个组件,但是在添加高程时遇到了 Style 的一些问题,它在View内而不是View外添加阴影

Here is my code这是我的代码

import {Text, View, StyleSheet} from 'react-native';
import {RadioButton} from 'react-native-paper';
import {
    ScrollView,
    TextInput,
    TouchableOpacity,
} from "react-native-gesture-handler";

function Chosen({values, onChange, defaultIndex, selectedIndexAtt, renderComponent}){
    const [index, setIndex] = useState(defaultIndex ? defaultIndex : 0);

    const getRadioStatus = (itemIndex) => {
        return index == itemIndex ? 'checked' : 'unchecked';
    }

    const handleRadioPress = (itemIndex) => {
        setIndex(itemIndex);
        onChange(itemIndex);
    }

    return(
        <ScrollView style={styles.container}>
            <View style={styles.containerRow}>
                {values.map((item, index)=> {
                    return (<View style={styles.itemContainer}>
                        <View style={styles.item}>
                            {renderComponent(item)}
                            <View style={styles.radioButton}>
                                <RadioButton value={item[selectedIndexAtt ? selectedIndexAtt : index]} 
                                    status={getRadioStatus(item[selectedIndexAtt ? selectedIndexAtt : index])}
                                    onPress={() => {
                                        handleRadioPress(item[selectedIndexAtt ? selectedIndexAtt : index])
                                    }}/>
                            </View>
                        </View>
                    </View>)
                })}
            </View>
        </ScrollView>
    );
}

const styles = StyleSheet.create({
    container:{
        flexDirection: 'column',
        flex: 1
    },
    containerRow:{
        flexDirection: 'row',
        flexWrap: 'wrap',
    },
    itemContainer: {
        width: "50%",
        padding: 1
    },
    item: {
        flexDirection: 'row',
        margin: 10,
        borderWidth: 1,
        borderColor: 'rgba(105,105,105,0.6)',
        borderRadius: 15,
        elevation: 0.1
    },
    radioButton: {
        justifyContent: 'center',
        flex: 1,
        alignItems: 'flex-end'
    }
});

export default Chosen;

and here is how I call it from the parent这就是我从父母那里称呼它的方式

<Chosen values={dummyData} defaultIndex={dummyData[0].id} selectedIndexAtt='id' renderComponent={(item) => {
                    return (
                        <View>
                            <Text>{item.name}</Text>
                        </View>
                    )
                }} onChange={onChange}></Chosen>

and the result is this:结果是这样的:

elevation shadow issue高程阴影问题

Can someone please help me with this?有人可以帮我吗?

You have to provide this for a shadow in react native.您必须为 react native 中的阴影提供此功能。 Change the values according to your need.根据需要更改值。

shadowColor: "#000",
shadowOffset: {
    width: 0,
    height: 10,
},
shadowOpacity: 0.5,
shadowRadius: 10,
elevation: 20,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM