Is it possible to get the size (width and height) of a certain view? For example, I have a view showing the progress:
function CalcDirectorySize($DirectoryPath) {
$Size = 0;
$Dir = opendir($DirectoryPath);
if (!$Dir)
return -1;
while (($File = readdir($Dir)) !== false) {
// Skip file pointers
if ($File[0] == '.') continue;
// Go recursive down, or add the file size
if (is_dir($DirectoryPath . $File))
$Size += CalcDirectorySize($DirectoryPath . $File . DIRECTORY_SEPARATOR);
else
$Size += filesize($DirectoryPath . $File);
}
closedir($Dir);
return $Size;
}
I need to know the actual width of the view to align other views properly. Is this possible?
答案 0 :(得分:226)
从React Native 0.4.2开始,View组件有一个onLayout
prop。传递一个接受事件对象的函数。事件的nativeEvent
包含视图的布局。
<View onLayout={(event) => {
var {x, y, width, height} = event.nativeEvent.layout;
}} />
每当调整视图大小时,也会调用onLayout
处理程序。
主要警告是,onLayout
处理程序首先在组件安装后的一帧中调用,因此您可能希望在计算布局之前隐藏UI。
答案 1 :(得分:14)
这是唯一对我有用的东西:
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Image
} from 'react-native';
export default class Comp extends Component {
find_dimesions(layout){
const {x, y, width, height} = layout;
console.warn(x);
console.warn(y);
console.warn(width);
console.warn(height);
}
render() {
return (
<View onLayout={(event) => { this.find_dimesions(event.nativeEvent.layout) }} style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native!
</Text>
<Text style={styles.instructions}>
To get started, edit index.android.js
</Text>
<Text style={styles.instructions}>
Double tap R on your keyboard to reload,{'\n'}
Shake or press menu button for dev menu
</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
AppRegistry.registerComponent('Comp', () => Comp);
答案 2 :(得分:9)
也许您可以使用measure
:
measureProgressBar() {
this.refs.welcome.measure(this.logProgressBarLayout);
},
logProgressBarLayout(ox, oy, width, height, px, py) {
console.log("ox: " + ox);
console.log("oy: " + oy);
console.log("width: " + width);
console.log("height: " + height);
console.log("px: " + px);
console.log("py: " + py);
}
答案 3 :(得分:7)
基本上,如果您想设置大小并进行更改,请将其设置为布局状态,如下所示:
import React, { Component } from 'react';
import { AppRegistry, StyleSheet, View } from 'react-native';
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'yellow',
},
View1: {
flex: 2,
margin: 10,
backgroundColor: 'red',
elevation: 1,
},
View2: {
position: 'absolute',
backgroundColor: 'orange',
zIndex: 3,
elevation: 3,
},
View3: {
flex: 3,
backgroundColor: 'green',
elevation: 2,
},
Text: {
fontSize: 25,
margin: 20,
color: 'white',
},
});
class Example extends Component {
constructor(props) {
super(props);
this.state = {
view2LayoutProps: {
left: 0,
top: 0,
width: 50,
height: 50,
}
};
}
onLayout(event) {
const {x, y, height, width} = event.nativeEvent.layout;
const newHeight = this.state.view2LayoutProps.height + 1;
const newLayout = {
height: newHeight ,
width: width,
left: x,
top: y,
};
this.setState({ view2LayoutProps: newLayout });
}
render() {
return (
<View style={styles.container}>
<View style={styles.View1}>
<Text>{this.state.view2LayoutProps.height}</Text>
</View>
<View onLayout={(event) => this.onLayout(event)}
style={[styles.View2, this.state.view2LayoutProps]} />
<View style={styles.View3} />
</View>
);
}
}
AppRegistry.registerComponent(Example);
你可以创建更多应该如何修改的变体,在另一个组件中使用它,将另一个视图作为包装器并创建一个onResponderRelease回调,它可以将触摸事件位置传递到状态,然后可以传递将子组件作为属性,可以覆盖onLayout更新状态,放置{[styles.View2, this.state.view2LayoutProps, this.props.touchEventTopLeft]}
等等。
答案 4 :(得分:5)
您可以通过 View
props 轻松获得 onLayout
的大小。
import React from 'react'
import { View } from 'react-native'
export default function index() {
const onLayout=(event)=> {
const {x, y, height, width} = event.nativeEvent.layout;
}
return (
<View onLayout={onLayout}>
<OtherComponent />
</View>
)
}
每当调整视图大小时,也会调用 onLayout
处理程序。
主要的警告是 onLayout
处理程序在您的组件安装后的一帧首先被调用,因此您可能希望隐藏您的 UI,直到您计算出您的布局。
答案 5 :(得分:0)
对我来说,将尺寸设置为使用%对我有用
width:'100%'
答案 6 :(得分:-2)
这是获取设备完整视图尺寸的代码。
var windowSize = Dimensions.get("window");
像这样使用它:
width=windowSize.width,heigth=windowSize.width/0.565