javascript - 如何在 React Native 中将图像居中放置在 View 中

标签 javascript css react-native

我有一个 GridView ,在每个单元格中我添​​加了一个 View ,然后添加了一个图像,图像没有居中代码如下

<ImageBackground
                source={require('./images/marble.jpg')}
                style={styles.backgroundImage}>

                <GridView
                    itemDimension={130}
                    items={items}
                    style={styles.gridView}
                    renderItem={item => (
                        <View style={[styles.itemContainer, { backgroundColor: item.code }]}>
                            <View style={styles.CircleShapeView}>
                            <Image style={styles.iconItem} source={item.name}/>
                            </View>
                        </View>
                    )}
                />

            </ImageBackground>

和样式是

const styles = StyleSheet.create({
    backgroundImage: {
        flex: 1,
        resizeMode: 'cover', // or 'stretch'
    },
    CircleShapeView: {
        width: 100,
        height: 100,
        borderRadius: 100,
        backgroundColor: '#00BCD4'
    },
    gridView: {
        paddingTop: 50,
        flex: 1,

    },
    itemContainer: {
        justifyContent: 'center',
        alignItems:'center',
        height:130
    },
    iconItem: {
        alignItems:'center',
        justifyContent: 'center'
    }
});

输出看起来像这样

enter image description here

图片应该在圆的中心,但它不是。

最佳答案

您的图像在“CircleShapeView” View 中

CircleShapeView: {
    width: 100,
    height: 100,
    borderRadius: 100,
    backgroundColor: '#00BCD4',
    justifyContent: 'center',
    alignItems: 'center'
}

关于javascript - 如何在 React Native 中将图像居中放置在 View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49150736/

相关文章:

javascript - 让 JS 检测页面上的位置?

Javascript,如何读取本地文件?

javascript - 捕获 Bluebird 中 setinterval 函数内部抛出的错误

css - Bootstrap 4 - 水平对齐内联列表

javascript - this.props.navigation 未定义(createStackNavigator)

javascript - 如何将 react-native-vanilla 项目迁移到 expo?

javascript - 如何获取我在 chrome.storage 中设置的对象数据?

css - 如何通过div向附近添加高度?

CSS 和 .less : Set a font color if user pick a specific color through web interface

javascript - firebase存储: Error: storage/unknown No content provider