简体   繁体   English

React Native-如何独立设置视图大小屏幕大小?

[英]React Native - how set View size screen size independently?

I'd like make View will be looked the same at different sizes (for Android 4+) and dpi (240+), as well IPhone 4+ and IPad. 我想让View在不同尺寸(对于Android 4+)和dpi(240+)以及iPhone 4+和iPad上看起来一样。 When I define size, ex. 当我定义尺寸时,例如 270x65, it looks double size in screen with 640 dpi than same size in screen 320 dpi (2560x1600 real screen size). 270x65,它在640 dpi的屏幕上看起来比在320 dpi的屏幕(2560x1600实际屏幕尺寸)上相同尺寸大一倍。

Is this a good solution? 这是一个好的解决方案吗?

  containerButton: {
    width: calcDimension(270),
    height: calcDimension(65)
  }

const calcDimension = size =>
  size / (screenScale() > 2 ? (screenScale() / 2) : 1);

const screenScale = () =>
  (Dimensions.get('window').scale);

Or there could be better solution? 还是有更好的解决方案? Thank you. 谢谢。

You can use Dimensions in react native to get device height and width calculated automatically. 您可以在react native中使用Dimensions以自动计算设备的高度和宽度。

First import Dimensions 首次导入尺寸

import { Dimensions, Platform, StyleSheet } from 'react-native

Get device height and width 获取设备的高度和宽度

var deviceHeight = Dimensions.get('window').height;
var deviceWidth = Dimensions.get('window').width;

Now you can use this global variable in your styles. 现在,您可以在样式中使用此全局变量。 for example: 例如:

const styles = StylSheet.create({
     mainContainer: {
          height: deviceHeight/2;
          width: deviceWidth/2;
     }
});

You can also dynamically calculate statusbar height and width standard way: 您还可以动态计算状态栏的高度和宽度标准方式:

var STATUS_BAR_HEIGHT = Platform.OS === 'ios' ? 20 : 25;
var HEADER_HEIGHT = Platform.OS === 'ios' ? 44 + STATUS_BAR_HEIGHT : 44 + STATUS_BAR_HEIGHT;

I have just gone through your query and i think it would be great if you try using "dimensions" . 我刚刚完成了您的查询,我认为如果您尝试使用“ dimensions”将会很棒。 Dimensions is another important parameter which is provided by react native it self. 尺寸是另一个重要参数,它是由自身反应提供的。 What you need to do is just import the dimension in your component. 您只需要在组件中导入尺寸即可。

var {height, width} = Dimensions.get('window');

The above command will set height and width of the device according no matter it is iphone or android. 上面的命令将根据设备是iphone还是android来设置设备的高度和宽度。

Just go throung the link: https://facebook.github.io/react-native/docs/dimensions.html 只需转到链接: https ://facebook.github.io/react-native/docs/dimensions.html

Cheers :) 干杯:)

you can use vw or vh to set the dimensions. 您可以使用vw或vh设置尺寸。

for example: height:"90vh", width:"50vw" will set height of the respective element 90% to the window size and width 50%. 例如:height:“ 90vh”,width:“ 50vw”会将各个元素的高度设置为窗口大小的90%,将窗口宽度设置为50%。

Hope this helped. 希望这会有所帮助。

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

相关问题 react-native - 适合包含视图的图像,而不是整个屏幕尺寸 - react-native - Fit Image in containing View, not the whole screen size 如何在react-native的动态列表视图中更改字体大小? - How to change font size in dynamic list view in react-native? 如何使 ScrollView 组件的大小与 React Native 中的屏幕大小成比例? - How Do I Make ScrollView Components Size a Ratio of Screen Size in React Native? 如何在反应导航中使用 header 将图像设置为全尺寸屏幕? - How to set Image to take the full size screen with header in react navigation? 如何设置反应原生的不同IOS设备的字体大小 - how to set font size for different IOS devices in react native 如何设置菜单大小固定在本机? - How can set menu size fixed in react native? React Native 设置字体大小以填充父级 - React Native set font size to fill parent 当显示大小更改时,如何在React Native中保持视图大小不变? - How to keep a view the same size in React Native when display size changes? 添加子视图时,React Native 父视图大小正在发生变化 - React Native parent view size is changing when adding a child view React Native在屏幕之间导航,屏幕尺寸缩小的错误 - React Native navigating between screens, screen size shrinking bug
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM