【问题标题】:Align icon to left and text to center in react native在本机反应中将图标左对齐,文本居中
【发布时间】:2017-05-21 20:16:43
【问题描述】:

我正在尝试使用 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',
    },

  });

【问题讨论】:

    标签: css react-native flexbox


    【解决方案1】:

    您可以通过将图标设置为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',
      },
    
    });
    

    【讨论】:

      【解决方案2】:

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

          <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,
        },
      

      【讨论】:

        【解决方案3】:

        试试这个:

        <Button style={signupStyles.facebookButton}>
              <View style={signupStyles.nestedButtonView}>
              <View style={signupStyles.iconstyle}>
                <Icon
                  name="facebook"
                  size={30}
                  color={'white'}
        
                />
              </View>
                <Text style={signupStyles.buttonText}>Continue with Facebook</Text>
              </View>
            </Button>
        
        var signupStyles = StyleSheet.create({
            buttonText: {
              color: 'white',
              fontWeight: 'bold',
              textAlign: 'center',
            },
            iconstyle:{
           // alignItems:'flex-start'
            }
        
            facebookButton: {
              backgroundColor: styleConstants.facebookColor,
              borderColor: styleConstants.facebookColor,
              borderWidth: 2,
              borderRadius: 22,
            },
        
            nestedButtonView: {
           //  flexDirection: 'row'
            },
        
          });
        

        【讨论】:

          猜你喜欢
          • 2019-10-01
          • 2017-10-20
          • 2017-06-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-01
          • 2019-09-28
          相关资源
          最近更新 更多