【问题标题】:React-Native: how to fill fullsize screen without explicit Width and Height?React-Native:如何在没有明确宽度和高度的情况下填充全屏?
【发布时间】:2017-05-25 00:47:06
【问题描述】:

我想创建一个 fullsize 屏幕,其子视图(视频播放器)在整个屏幕上呈现。只有当我明确地将 widthheight 传递给组件时,我才能让它工作。

但我知道有一个名为“flex”的属性。在许多教程中,他们会执行“flex: 1”之类的操作,但对我来说,它没有做它应该做的事情。

(为了完整起见,视频播放器不是问题的一部分。我也可以用<Image> 或其他类型的视图替换<video> 标签并获得相同的结果)

render() {
    const uri = this.props.uri
    return (
        <KeyboardAwareScrollView keyboardShouldPersistTaps="always" >
            <TouchableWithoutFeedback onPress={RouterActions.pop}>
                <Video source={{uri: uri}}   
                    ref={(ref) => {
                        this.player = ref
                    }}                       
                    style={s.fullsize} 

                />
            </TouchableWithoutFeedback>

            <TouchableOpacity onPress={RouterActions.pop} style={s.closeBtn}>
                <Icon name="times-circle-o" size={20} color="white" />
            </TouchableOpacity>


        </KeyboardAwareScrollView>
    )
}

我的风格:

这只有在我通过宽度和高度时才有效:

const s = StyleSheet.create({
  fullsize: {
    backgroundColor: 'black',
    //flex: 1,
    width: Dimensions.get('window').width,
    height: Dimensions.get('window').height,
  },
  closeBtn: {
      position: 'absolute',
      left: 20,
      top: 25, 
  },

});

我只尝试了这个,但是屏幕会是空的,因为 -Component 的宽度和高度都是 0。

const s = StyleSheet.create({
  fullsize: {
    backgroundColor: 'black',
    flex: 1,   // this is not working
    left: 0,
    right: 0,
    top: 0,
    bottom: 0
  },
});

【问题讨论】:

    标签: react-native flexbox


    【解决方案1】:

    我相信flex: 1 会占用它的父元素提供的所有空间。在你的情况下,你的父元素都没有任何样式,所以试试这个:

    render() {
        const uri = this.props.uri
        return (
            <View style={{ flex: 1 }}>
                <TouchableWithoutFeedback style={{ flex: 1 }} onPress={RouterActions.pop}>
                    <Video source={{uri: uri}}   
                        ref={(ref) => {
                            this.player = ref
                        }}                       
                        style={{ flex: 1 }} 
    
                    />
                </TouchableWithoutFeedback>
    
                <TouchableOpacity onPress={RouterActions.pop} style={s.closeBtn}>
                    <Icon name="times-circle-o" size={20} color="white" />
                </TouchableOpacity>
            </View>
        )
    }
    

    【讨论】:

    • 扩展 Matt 的答案 - flex 值是基于优先级的布局指标。例如,如果您有两个同级视图,其中视图 A 的 flex 为 1,视图 B 的 flex 为 2,则视图 B 将是父区域的 2/3,A 是 1/3。值得查看有关布局和 flex 的 RN 文档:facebook.github.io/react-native/docs/flexbox.html
    猜你喜欢
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 2017-01-22
    相关资源
    最近更新 更多