加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

react-native – 如何在本机中获取元素的宽度?

发布时间:2020-12-15 20:46:18 所属栏目:百科 来源:网络整理
导读:如何获得一个元素的宽度作为反应原生如View? 由于反应原生的宽度没有百分比用量,如何获得元素或父元素的宽度? 您可以调用onLayout事件来测量元素: measureView(event) { console.log('event properties: ',event); console.log('width: ',event.nativeEve
如何获得一个元素的宽度作为反应原生如View?
由于反应原生的宽度没有百分比用量,如何获得元素或父元素的宽度?
您可以调用onLayout事件来测量元素:
measureView(event) {
  console.log('event properties: ',event);
  console.log('width: ',event.nativeEvent.layout.width)
}

<View onLayout={(event) => this.measureView(event)}>

就宽度和高度百分比而言,您可以获得窗口宽度和高度,并使用它们:

var {
   ...
   Dimensions
} = React

const width = Dimensions.get('window').width
const height = Dimensions.get('window').height

// 80% width
width: width * .8,// 25% height
height: height / 4,

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读