将水平线移到StyledSheets中的中心

时间:2020-06-17 12:51:14

标签: javascript css reactjs typescript react-native

我已经画了一条这样的水平线。但是,它显示在屏幕的左侧。我不想增加宽度。我还能如何将其移动到中心?我尝试将其与alignContent:'center'等其他视图一起包装,但这对我不起作用。

<View style={styles.horizontalLine}></View>
  horizontalLine: {
    marginTop: 25,
    width: '80%',
    height: 1,
    //alignContent: 'center',
    //width: 1,
    backgroundColor: '#E0E0E0',
  },

enter image description here

3 个答案:

答案 0 :(得分:1)

添加一个alignSelf:'center',如下所示

 horizontalLine: {
    marginTop: 25,
    width: '80%',
    height: 1,
    //alignContent: 'center',
    //width: 1,
    alignSelf: 'center',
    backgroundColor: '#E0E0E0',
  }

答案 1 :(得分:1)

您可以设置水平边距:自动

horizontalLine: {
  // ...
  marginLeft: 'auto',
  marginRight: 'auto',
  // ...
},

答案 2 :(得分:0)

尝试一下...

horizontalLine: { margin: 'auto', marginTop: 25, width: '80%', height: 1, backgroundColor: '#E0E0E0', }