[英]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.