React Native 中具有绝对位置的中心活动指示器

Cos*_*ure 2 css activity-indicator react-native

我正在使用视图叠加和 ActivityIndi​​cator 在后台隐藏加载地图。我使用的解决方案是具有绝对位置的 View ,使其位于所有内容之上,并将 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        
    }
Run Code Online (Sandbox Code Playgroud)

这使得指示器偏离中心。我可以在那里放置一些神奇的数字,使其适用于特定设备,猜测指示器的大小,但它不适用于其他设备。谢谢你的帮助。

hon*_*lop 5

如果你想让ActivityIndi​​cator在屏幕上居中,你可以这样设置样式。

      <View style={{ flex: 1, alignItems: "center", justifyContent: "center", zIndex: 20 }}>
        <ActivityIndicator
          size="large" color={colors.color4}
        />
      </View>
Run Code Online (Sandbox Code Playgroud)

或者你可以使用样式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'      
 }
Run Code Online (Sandbox Code Playgroud)