简体   繁体   English

react-native: 在屏幕中央显示微调器

[英]react-native:Show spinner at the center of the screen

I want to show the spinner at the center of screen.I used alignItems, justifyContent and flex but there is no change in the position then i use margintop for correct positioning.why there is no change on the use of alignItems,justifyContent and flex?我想在屏幕中央显示微调器。我使用了 alignItems、justifyContent 和 flex,但位置没有变化,然后我使用 margintop 进行正确定位。为什么 alignItems、justifyContent 和 flex 的使用没有变化?

here im using spinner.我在这里使用微调器。

 return (
  <View style={style.spinnerStyle}>
  <Spinner size ="large" />
  </View>
 );
 const style = {
spinnerStyle: {
  flex: 1,
  marginTop:240
}
};

Spinner.js Spinner.js

  import React from 'react';
  import {View,ActivityIndicator} from 'react-native';

  const Spinner = ({size}) => {
  return (
  <View style = {styles.spinnerStyle}>
  <ActivityIndicator size={size || 'large'} />
  </View>
  );
 };

const styles = {
spinnerStyle: {
flex: 1,
justifyContent: 'center',
alignItems:'center'
 }
 };

export { Spinner };

Can you please try with following:您可以尝试以下操作:

where you are using Spinner你在哪里使用 Spinner

spinnerStyle: {
    flex: 1,
    marginTop:240,
    justifyContent: 'center',
    alignItems:'center'
}

In Spinner在微调器中

spinnerStyle: {
    flex: 1
    alignSelf:'center'
}
const styles = StyleSheet.create({
    spinnerStyle: { 
    position: 'absolute',
    left: 0,
    right: 0,
    top: 0,
    bottom: 0,
    alignItems: 'center',
    justifyContent: 'center'
 }
});

I want to show the spinner at the center of screen.I used alignItems, justifyContent and flex but there is no change in the position then i use margintop for correct positioning.why there is no change on the use of alignItems,justifyContent and flex?我想在屏幕中央显示微调框。我使用alignItems,justifyContent和flex,但是位置没有变化,所以我使用margintop进行正确定位。为什么对alignItems,justifyContent和flex的使用没有变化?

here im using spinner.我在这里使用微调器。

 return (
  <View style={style.spinnerStyle}>
  <Spinner size ="large" />
  </View>
 );
 const style = {
spinnerStyle: {
  flex: 1,
  marginTop:240
}
};

Spinner.js Spinner.js

  import React from 'react';
  import {View,ActivityIndicator} from 'react-native';

  const Spinner = ({size}) => {
  return (
  <View style = {styles.spinnerStyle}>
  <ActivityIndicator size={size || 'large'} />
  </View>
  );
 };

const styles = {
spinnerStyle: {
flex: 1,
justifyContent: 'center',
alignItems:'center'
 }
 };

export { Spinner };

The correct way to vertically center the ActivityIndicator component is to use 'flex: 1' on the ActivityIndicator component itself, not on its parent component.将 ActivityIndi​​cator 组件垂直居中的正确方法是在 ActivityIndi​​cator 组件本身上使用 'flex: 1',而不是在其父组件上。 It does not need a wrapper component and none of the other flex alignment styles are necessary at all.它不需要包装器组件,并且根本不需要其他任何 flex 对齐样式。

The improved implementation of the OP's code would be: OP代码的改进实现将是:

import React from 'react';
import {ActivityIndicator} from 'react-native';

const Spinner = ({size}) => {
  return (
    <ActivityIndicator size={size || 'large'} style={{ flex: 1 }}/>
  );
 };

export { Spinner };

Set in spinner parent view:在微调父视图中设置:

justifyContent: 'center',
alignItems:'center'

And in spinner set:在微调组中:

alignSelf:'center'   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM