【问题标题】:React native image keep centered反应原生图像保持居中
【发布时间】:2018-12-11 13:39:34
【问题描述】:

我有一个简单的屏幕,页面底部有一个图像和一些文本 + 一个按钮。在纵向模式下它看起来不错,但在横向模式下,图像、文本和按钮无法正确显示。 当高度不够时,我试图让图像调整大小,但我找不到办法。

 render() {        
    return (
        <View style={styles.container}>
            <View style={styles.bodyContainer}>
                <Image style={styles.image}
                    source={require('./../../assets/images/server-info-icon.png')}
                />
                <Text style={styles.serverDetailsText}>Server details</Text>
                <Text style={styles.instructionText}>instruction</Text>
            </View>
            <View style={styles.buttonContainer}>
                <TouchableHighlight style={styles.gotItButton}
                        onPress={() => this.props.navigation.goBack(null)}>
                    <Text style={styles.gotItButtonText}>GOT IT</Text>
                </TouchableHighlight>
            </View>
        </View>
    );
};

const styles = StyleSheet.create({
container: {
    flex: 1,
    backgroundColor: '#ffffff'
},
bodyContainer: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center'
},
buttonContainer: {
    height: 82
},
gotItButton: {
    backgroundColor: COLORS.BUTTONS_COLOR,
    height: 46,
    width: 288,
    borderRadius: 25,
    alignSelf: 'center',
    position: 'absolute',
    bottom: 18
},
gotItButtonText: {
    textAlign: 'center',
    paddingTop: 11,
    paddingBottom: 13,
    fontSize: 17,
    fontFamily: CONSTANTS.DEFAULT_FONT,
    fontWeight: CONSTANTS.TEXT_BOLD_WEIGHT,
    color: COLORS.BUTTONS_TEXT_COLOR
},
image: {
    maxHeight: 170,
    maxWidth: 361,
    marginBottom: 5
},
instructionText: {
    color: COLORS.LABELS_COLOR,
    fontSize: 15,
    fontFamily: CONSTANTS.DEFAULT_FONT,
    height: 36,
    width: 206,
    flexWrap: 'wrap',
    textAlign: 'center',
    marginTop: 2,
    marginBottom: 2
},
serverDetailsText: {
    color: COLORS.TEXT_COLOR,
    fontSize: 17,
    fontFamily: CONSTANTS.DEFAULT_FONT,
    fontWeight: CONSTANTS.TEXT_BOLD_WEIGHT,
    width: 206,
    textAlign: 'center',
    marginTop: 2,
    marginBottom: 2
}

})

【问题讨论】:

  • 检查height并相应地改变你的风格
  • 不要在图像属性上使用 maxHeight。请使用弹性
  • 所以你说要根据屏幕的高度动态设置它。有没有办法自动做?
  • @Aboo Pallikara 你能给我举个例子吗?

标签: image react-native styles size center


【解决方案1】:

示例代码

<Image style={{flex: 1,
           width: null,
           height: null}}
           source={require('myimage')}
/>

请使用 flex 样式添加外部视图。然后 Inner Flex 变化为 0.1 到 1。

【讨论】:

  • 我尝试了你的方法,但我无法将高度和宽度设置为 null,因为图像消失了。相反,现在我有以下风格:image: { maxHeight: 170, maxWidth: 361, flex: 1, marginBottom: 5 },。当模式为横向时,使用这种风格我仍然有同样的问题。图片在导航栏下
  • 如果我设置了一个marginTop,它可以工作,但我不喜欢这种方法。无论如何,这是另一个问题。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-22
  • 2018-02-08
  • 1970-01-01
  • 1970-01-01
  • 2018-02-27
  • 2018-05-04
相关资源
最近更新 更多