(React Native) 如何设置边距或填充或宽度或高度?

时间:2021-03-15 23:30:40

标签: react-native

<View style={width: 20}>Hello</View>

平台操作系统?

Dimensions.get("screen") ? -> 1100, 1440 ... ?

如何分隔宽度? 我希望它在所有设备(包括 Android 和 iPhone)上看起来都一样。

2 个答案:

答案 0 :(得分:1)

如果您想设置响应式width,或随设备width而变化的width,请使用Dimension。看看下面的代码:

import {Dimension, StyleSheet} from 'react-native`

.....
<View style={styles.container}>Hello</View>
.....

const styles = StyleSheet.create({
      container:{
          width: Dimension.get('screen').width, // the container will take the whole width of the device screen width
       }
 })

如果您得到答复,请告诉我

答案 1 :(得分:0)

您通常不需要获取平台信息来制作响应式 UI。 您可以简单地导入 "Dimensions" 并从中获取宽度。

示例:

import {Dimension} from "react-native"

//for width
const SCREEN_WIDTH = Dimensions.get('screen').width;

//for height
const SCREEN_HEIGHT = Dimensions.get('screen').height;