React native 如何设置父视图';s基于子视图的高度';s高度
我想编写一个自定义的父视图React native 如何设置父视图';s基于子视图的高度';s高度,react-native,React Native,我想编写一个自定义的父视图视图,其中包括一个文本组件子视图或两个文本组件子视图。有没有办法根据文本视图的高度设置父视图的高度 class ParentView extends Component { constructor(props) { super(props); } render() { const { titleText, bodyText, } = this.props; //if titleText is pas
视图
,其中包括一个文本
组件子视图或两个文本
组件子视图。有没有办法根据文本视图的高度设置父视图的高度
class ParentView extends Component {
constructor(props) {
super(props);
}
render() {
const {
titleText,
bodyText,
} = this.props;
//if titleText is passed to props, measure it's height;
//if bodyText is passed to props, measure it's height;
// set contentContainer height = titleText + bodyText + someMargin
return (
<View style={styles.contentContainer}>
{titleText && <Text style={styles.title}>
{titleText}
</Text>}
{bodyText && <Text style={styles.body}>
{bodyText}
</Text>}
</View>
);
}
}
类ParentView扩展组件{
建造师(道具){
超级(道具);
}
render(){
常数{
标题文本,
正文,
}=这是道具;
//如果titleText传递给支柱,则测量其高度;
//如果bodyText传递给道具,请测量其高度;
//设置contentContainer高度=titleText+bodyText+someMargin
返回(
{titleText&&
{titleText}
}
{bodyText&&
{bodyText}
}
);
}
}
您可以在视图
容器上设置flex:0
const styles = StyleSheet.create({
contentContainer: {
flex: 0
}
});
用这种方法,它会根据孩子的身高而扩展
有关flex
属性的一些信息:
当flex是一个正数时,它使组件灵活,并且
其大小将与其弹性值成比例。因此,我们需要一个具有flex的组件
设置为2将占用两倍的空间作为flex设置为1的组件
当flex为0时,将根据宽度和高度调整构件的大小
而且它是不灵活的
当flex为-1时,组件通常根据宽度和宽度调整大小
高度。但是,如果没有足够的空间,组件将
收缩到最小宽度和最小高度
为什么要设置家长的身高?您可以简单地省略高度、最小/最大和弹性样式,以便父级将包装其子级。您尝试过任何给定的答案吗?谢谢关于flex:0
。它救了我的命:v