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

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

问题描述

我正在为一个开源项目做贡献,我正在为 React Native 开发材料设计.我在工作中被阻止,我无法进行一些 UI 级别的增强 w.r.t.填充、对齐等,

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-

在上图中,测量的UNITdp.

但是,在我的 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 会在像素密度高的屏幕上渲染得更厚.这听起来很合乎逻辑,因为您在高密度屏幕上的精度比在低密度屏幕上的精度更高,并且 react-native 的目标是通用,因此它不会假设高 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天全站免登陆