【问题标题】:Vertical centering in React Native doesn't workReact Native 中的垂直居中不起作用
【发布时间】:2018-06-01 17:20:08
【问题描述】:

我使用ScrollView,但无法将图标居中在内部Views 之一中。

    import Icon from 'react-native-vector-icons/MaterialCommunityIcons';

<ScrollView>
...
        <View style={styles.detailRowContainer}>
          <View style={{flex: 1}}>
            <Text style={styles.label} numberOfLines={1}>
              {'Phone Number'}
            </Text>
            <Text style={styles.value} numberOfLines={1}>
              {phone}
            </Text>
          </View>
          <View style={styles.round}>
            <TouchableWithoutFeedback onPress={this._onPressPhone}>
              <Icon size={22} name="phone" />
            </TouchableWithoutFeedback>
          </View>
        </View>;
...
</ScrollView>



        detailRowContainer: {
            flexDirection: 'row',
            justifyContent: 'center',
            flex: 1,
            marginTop: 10,
            paddingBottom: 10,
            borderBottomWidth: 1,
            borderBottomColor: Colors.lightGray,
        },
        label: {
            color: Colors.glofoxDark,
            marginBottom: 3,
        },
        value: {
            color: Colors.glofoxDark,
            fontWeight: '800',
            borderBottomWidth: 1,
            borderBottomColor: Colors.lightGray,
        },
        round: {
            backgroundColor: Colors.lightBlue,
            width: 30,
            height: 30,
            borderRadius: 30,
            overflow: 'hidden',
            justifyContent: 'center',
            alignItems: 'flex-end',
            flexDirection: 'row',
            padding: 4,
        },

【问题讨论】:

    标签: react-native flexbox


    【解决方案1】:

    样式需要这样修改。

    现在你正在做

    • flexDirection: row 沿 justifyContent: center。由于您的第一个子元素采用了完整的 parent flex,因此它不会显示其效果
    • paddingBottom 已给出,但为了居中,必须给出等效的 paddingTop
    • 圆形样式的padding 应替换为margin,否则会影响内部元素的位置
    • alignItems in round ,不能是flex-end,应该换成center

    这里是固定垂直居中的样式

     detailRowContainer: {
            flexDirection: 'row',
            alignItems: 'center',
            flex: 1,
            marginTop: 10,
            paddingTop: 10,
            paddingBottom: 10,
            borderBottomWidth: 1,
            borderBottomColor: Colors.lightGray,
        },
    
     round: {
            backgroundColor: Colors.lightBlue,
            width: 30,
            height: 30,
            borderRadius: 30,
            overflow: 'hidden',
            justifyContent: 'center',
            alignItems: 'center',
            margin: 4
        },
    

    【讨论】:

    • 如果我像这样更改填充,图标包装位置为the same as before,图标为not centered
    • 更新了答案
    • 感谢您的分析。我不知道填充会影响内部元素的位置
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-14
    • 1970-01-01
    相关资源
    最近更新 更多