react-native---rn中如何让组件居中定位,loading如何向H5中fixed绝对居中定位

rn中没有fixed定位,更没有100%宽高;
我们可以Dimensions以及flex布局进行居中设置。。。

//1. 把要居中定位的组件用view包起来(下面的样式便是针对外层view所写);
//2. 使用时候,将整个组件放在所有使用页面根组件下;

//如:

水平居中:

width:Dimensions.get('window').width, //窗口宽度
justifyContent:'center',
alignItems:'center',
position:'absolute', //定位

这里写图片描述

垂直居中:

height:Dimensions.get('window').height, //窗口高度
justifyContent:'center',
alignItems:'center',
position:'absolute',

这里写图片描述

绝对居中:

height:Dimensions.get('window').height,
width:Dimensions.get('window').width,
justifyContent:'center',
alignItems:'center',
position:'absolute',

这里写图片描述

示例代码:

import React from 'react';

import {
  ActivityIndicator,
  View,
  Text,
  StyleSheet,
  Dimensions,
} from 'react-native';

import colors from '../utils/color'

export default class Loading extends React.Component{
  render(){
    return(
      <View style={loadStyles.wrapper}>
        <View style={loadStyles.box}>
          <ActivityIndicator 
            animating={true}
            color={colors.white}
            size='large'
          />
          <Text style={loadStyles.txt}>数据加载中...</Text>
        </View>
      </View>
    )
  }
}

const loadStyles=StyleSheet.create({
  wrapper:{
    justifyContent:'center',
    alignItems:'center',
    position:'absolute',
    height:Dimensions.get('window').height,
    width:Dimensions.get('window').width,
    zIndex:10,

  },
  box:{

    paddingVertical:12,
    paddingHorizontal:20,
    flexDirection:'row',
    justifyContent:'center',
    alignItems:'center',
    backgroundColor:'rgba(0,0,0,0.6)',
    borderRadius:6
  },
  txt:{
    marginLeft:20,
    fontSize:14,
    color:colors.white
  }
})
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值