【问题标题】:React Native: How to set height of the child view same as parent view with position=absolute?React Native:如何将子视图的高度设置为与位置=绝对的父视图相同?
【发布时间】:2016-06-28 06:22:15
【问题描述】:

我想在父视图中显示两个子视图。两个孩子都有 position: "absolute" 的风格,因为我想在视图的位置上应用一些动画。我想将视图的高度设置为等于父视图。如何在 React Native 中实现这一点?

【问题讨论】:

    标签: react-native


    【解决方案1】:

    要让使用绝对位置的子视图与父视图具有相同的高度,您可以将它们的 topbottom 位置设置为 0。

    那么你仍然可以申请margintransform 来进一步改变他们的位置。

    var SampleApp = React.createClass({
      render: function() {
        return (
          <View style={styles.container}>
            <View style={styles.child1}/>
            <View style={styles.child2}/>
          </View>
        );
      }
    });
    
    var styles = StyleSheet.create({
      container: {
        flex: 1,
        backgroundColor: '#F5FCFF',
      },
      child1: {
        position: 'absolute',
        backgroundColor: 'green',
        right: 0,
        top: 0,
        bottom: 0,
        width: 40,
      },
      child2: {
        position: 'absolute',
        backgroundColor: 'blue',
        left: 0,
        top: 0,
        bottom: 0,
        width: 40,
      },
    });
    

    现场观看:https://rnplay.org/apps/kSZnBg

    【讨论】:

    • 这是一种方法。但是,视图必须始终与父边框完全对齐,例如 top: 0 和 bottom: 0 。这将阻止我做改变孩子位置的动画。我在我的问题中提到了这个要求。
    • 不,您仍然可以应用边距或变换,如我的回答中所述。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    • 2011-03-18
    • 1970-01-01
    相关资源
    最近更新 更多