首页 > 解决方案 > 在 React Native 中具有绝对位置的中心活动指示器

问题描述

我正在使用 View overlay 和 ActivityIndi​​cator 在后台隐藏加载地图。我正在使用的解决方案是使用绝对位置查看以使其位于所有内容之上并将活动指示器放在顶部,因此发生了一些事情。问题是,因为 ActivityIndi​​ator 大小不能设置为 iOS 上的特定像素,我不知道它的大小,因此我无法正确居中。或者我没有想出办法来做到这一点。

这是一些代码:

<View style={styles.whiteOverlay}>
      <ActivityIndicator style={styles.indicator} size="large" color={colors.color4} />
 </View>

 whiteOverlay: {
        width: screen.height,
        height: screen.height,  
        backgroundColor: 'white',
        position: 'absolute',     
        zIndex: 20        
    },
    indicator: {
        zIndex: 21,
        position: 'absolute',
        left: screen.width/2,
        top: screen.height/2        
    }

这会使指标偏离中心位置。我可以放一些神奇的数字让它适用于特定的设备,猜测指标的大小,但它在其他设备上不起作用。谢谢你的帮助。

标签: cssreact-nativeactivity-indicator

解决方案


如果要将 ActivityIndi​​cator 在屏幕上居中,可以像这样设置样式。

      <View style={{ flex: 1, alignItems: "center", justifyContent: "center", zIndex: 20 }}>
        <ActivityIndicator
          size="large" color={colors.color4}
        />
      </View>

或者您可以使用样式的position

      <View style={styles.whiteOverlay}  >
        <ActivityIndicator
          size="large" color={colors.color4} 
        />
      </View >
...
 whiteOverlay: {
    position: 'absolute',
    left: 0,
    right: 0,
    top: 0,
    bottom: 0,
    alignItems: 'center',
    justifyContent: 'center'      
 }

推荐阅读