在本机反应中将图标左对齐,文本居中

新手上路,请多包涵

我正在尝试使用 flexbox 将我的图标与按钮视图的左侧对齐,并将文本与中心对齐。目前它们都与中心对齐,但我不确定如何将我的图标放在按钮的最左边缘,同时保持文本在视图中居中

现在我的按钮看起来像这样:

在此处输入图像描述

我使用 https://github.com/APSL/react-native-button 作为按钮,使用 https://github.com/oblador/react-native-vector-icons 作为图标

下面是我的一些代码:

     <Button style={signupStyles.facebookButton}>
      <View style={signupStyles.nestedButtonView}>
        <Icon
          name="facebook"
          size={30}
          color={'white'}

        />
        <Text style={signupStyles.buttonText}>Continue with Facebook</Text>
      </View>
    </Button>

    var signupStyles = StyleSheet.create({
    buttonText: {
      color: 'white',
      fontWeight: 'bold',
      textAlign: 'center',
    },

    facebookButton: {
      backgroundColor: styleConstants.facebookColor,
      borderColor: styleConstants.facebookColor,
      borderWidth: 2,
      borderRadius: 22,
    },

    nestedButtonView: {
     flexDirection: 'row',
     alignItems: 'center',
    },

  });

原文由 Stone Preston 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 539
2 个回答

您可以通过将图标设置为 width 并给出文本 padding-righttext-alignflex:1

 <Button style={signupStyles.facebookButton}>
  <View style={signupStyles.nestedButtonView}>
    <Icon
      name="facebook"
      size={30}
      color={'white'}
    />
    <Text style={signupStyles.buttonText}>Continue with Facebook</Text>
  </View>
</Button>

var signupStyles = StyleSheet.create({
  buttonText: {
    color: 'white',
    fontWeight: 'bold',
    textAlign: 'center',
  },

  facebookButton: {
    backgroundColor: styleConstants.facebookColor,
    borderColor: styleConstants.facebookColor,
    borderWidth: 2,
    borderRadius: 22,
  },

  nestedButtonView: {
    flexDirection: 'row',
    alignItems: 'center',
  },

  iconLeft: {
    width: '40px',
  },

  buttonText: {
    flex: 1,
    paddingRight: '40px',
    textAlign: 'center',
  },

});

原文由 Asons 发布,翻译遵循 CC BY-SA 3.0 许可协议

我让它看起来有点像黑客。我在文本右侧添加了另一个图标,并使其与背景颜色相同。然后我给了我的按钮文本 2 的 flex。如果有人有更好的方法,我很想知道

    <Button style={signupStyles.facebookButton}>
      <View style={signupStyles.nestedButtonView}>
        <Icon
          name="facebook"
          size={30}
          color={"white"}
          style={signupStyles.iconLeft}
        />
        <Text style={signupStyles.buttonText}>Continue with Facebook</Text>
        <Icon
          name="facebook"
          size={30}
          color={styleConstants.facebookColor}
          style={signupStyles.iconRight}
        />
      </View>
    </Button>

造型:

 buttonText: {
    color: 'white',
    fontWeight: 'bold',
    textAlign: 'center',
    flex: 2
  },

  iconLeft: {
    marginLeft: 10,
  },

  iconRight: {
    marginRight: 10,
  },

原文由 Stone Preston 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题