0
点赞
收藏
分享

微信扫一扫

RN获取屏幕高宽、自适应(rem)设置

young_d807 2022-04-01 阅读 265

RN获取屏幕高宽、自适应(rem)设置

1、引入Dimensions
2、通过Dimensions.get(“window”).width/height; 获取屏幕高宽

pxToDp自适应js文件:
import {Dimensions} from 'react-native';
// 获取竖屏模式的宽度
const deviceWidthDp = Dimensions.get('window').width;
// UI 默认给图是750
const uiWidthPx = 750;
//传入设计稿宽度
function pxSize(uiElementPx) {
	return uiElementPx * deviceWidthDp / uiWidthPx;
}
export default pxToDp;
reactDomjs文件:
import React, {Component} from "react";
import { StyleSheet, View } from "react-native";
import pxToDp from '../res/pxToDp.js';
class reactDom extends React.Component {
    constructor(props) {
        super(props);
        this.state = {};
    }
    componentDidMount() {
        //
    }
    render() {
        return (
            <View style={styles.container}></View>
        );
    }
}
var styles = StyleSheet.create({
    container: {
        width: pxToDp(750),
        height: pxToDp(600)
    }
});
export default reactDom;
举报

相关推荐

0 条评论