React Native中样式的默认样式单位是什么? [英] What is the default unit of style in React Native?

查看:227
本文介绍了React Native中样式的默认样式单位是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个开源项目做贡献,在那里我正在为React Native开发Material Design.我在工作中受阻,无法进行一些UI级别的增强.填充,对齐等.

I am contributing to an Open Source Project where I am developing Material design for React Native. I am blocked at work,I am unable to make some UI level enhancements w.r.t. padding, alignment etc.,

这是抽屉材料设计的官方规格-

This is the Official Spec of Material Design for Drawer-

在上图中,测量的 UNIT dp .

但是,在我的React Native代码中,我没有提到任何此类单元.考虑到它是"react native",我很困惑它是 px 还是 dp .

But, in my React Native code, I see there is no such units mentioned. Considering it is "react native" I am confused whether it is px or dp.

我什至查看了 React Native for Style 的官方文档成分.我什么都看不到.

I even went over the Official Docs of React Native for Style component. I don't see a mention anywhere.

我的代码看起来像-

const styles = {
    touchable: {
        paddingHorizontal: 16,
        marginVertical: 8,
        height: 48
    },
    item: {
        flex: 1,
        flexDirection: 'row',
        alignItems: 'center',
    },
    icon: {
        position: 'relative',
    },
    value: {
        flex: 1,
        paddingLeft: 34,
        top: 2
    },
    label: {
        top: 2
    }
},

请问这是像素还是dp?还有1px = 1dp吗?

Please can you tell me, if this is pixels or dp? And also, is 1px = 1dp?

推荐答案

我有些困惑,无法像我们在浏览器中那样经常使用开发者控制台进行主动检查.

I share your confusion somewhat, not being able to actively inspect with a developer console as we are used to in the browser.

我对'dp'单元并不熟悉,但是根据我收集到的内容,width: 1在每个设备上的渲染方式都不同,具体取决于屏幕的像素密度(请参阅链接). react-native文档中的信息说,1将在具有高像素密度的屏幕上渲染更厚的图像.这听起来很合乎逻辑,因为您在高密度屏幕上比在低密度屏幕上具有更高的精度,并且本机目标是通用化,因此不会采用高dpi.

I am not familiar with the 'dp' unit, but from what I gather width: 1 renders differently on each device depending on the pixel density of the screen (see link). The information in the react-native docs say that 1 would render thicker on screens with high pixel density. Which then sounds logical as you have more precision on high density screens than you would have on low density screens and react-native aims at being universal so it would not assume high dpi.

据我了解,您可以使用下面链接的PixelRatio API来计算细节元素(如边框,图标等)的大小,从而可以根据设备的屏幕密度动态调整渲染的大小.

It is my understanding that you can use the below linked PixelRatio API to calculate sizes for detail elements (think borders, icons, etc), that way you can dynamically adjust the rendered size according to the device's screen density.

https://facebook.github.io/react-native/docs/pixelratio.html#content

这篇关于React Native中样式的默认样式单位是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆