当视图放置在 ScrollView 中以进行本机反应时,为什么 flexDirection 不起作用?

Ng *_*Qin 6 css scrollview flexbox react-native

当视图放置在 ScrollView 中以进行本机反应时,为什么 flexDirection 不起作用?

在此处输入图片说明

当我的视图未放置在滚动视图中时,参数 flexDirection: 'row' 工作正常。

export default class ProfileScreen extends Component {
render() {
    return (
            <View style={{flex: 0.1, flexDirection: 'row', justifyContent: 'flex-start', height:70}}>
                <View style={{flex:0.2, backgroundColor: 'red'}} />
                <View style={{flex:0.8, backgroundColor: 'skyblue'}} />
                <View style={{flex:0.2, backgroundColor: 'steelblue'}} />
            </View>
    );
}
Run Code Online (Sandbox Code Playgroud)

当它被放置在滚动视图中时,参数 flexDirection 不再起作用。

export default class ProfileScreen extends Component {
render() {
    return (
        <ScrollView stickyHeaderIndices={[0]} >
            <View style={{flex: 0.1, flexDirection: 'row', justifyContent: 'flex-start', height:70}}>
                <View style={{flex:0.2, backgroundColor: 'red'}} />
                <View style={{flex:0.8, backgroundColor: 'skyblue'}} />
                <View style={{flex:0.2, backgroundColor: 'steelblue'}} />
            </View>
            <View style={{flex: 1, flexDirection: 'row', height: 10}} />
            <View style={{flex: 1, flexDirection: 'row', height: 200, backgroundColor: 'skyblue'}} />
            <View style={{flex: 1, flexDirection: 'row', height: 10}} />
        </ScrollView>
    );
}
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

小智 13

我遇到了这个问题。

想想会发生什么,stickyHeaderIndices覆盖了受影响视图中的一些样式。简单地包装你想要坚持的视图将解决这个问题,即

...
  <ScrollView stickyHeaderIndices={[0]} >
    <View>
      <View style={{flex: 0.1, flexDirection: 'row', ... }}>
         <View style={{flex:0.2, backgroundColor: 'red'}} />
         <View style={{flex:0.8, backgroundColor: 'skyblue'}} />
         <View style={{flex:0.2, backgroundColor: 'steelblue'}} />
       </View>
    </View>
    
   ...
   </ScrollView>
Run Code Online (Sandbox Code Playgroud)


Rya*_*leh 7

将 flexDirection 放在 contentContainerStyle 中,试试这样:

 <ScrollView style={{Your Style}} contentContainerStyle={{flexDirection:'row'}}>

   <View> 

  </View>

  <View> 

  </View>

</ScrollView>
Run Code Online (Sandbox Code Playgroud)


Ng *_*Qin 2

这段代码的工作原理:

export default class ProfileScreen extends Component {
render() {
    return (
        <ScrollView stickyHeaderIndices={[0]} style={{flex:1}}>
            <View style={{flex: 0.1, flexDirection: 'row', justifyContent: 'flex-start', height:70}}>
                <View style={{flex: 1, flexDirection: 'row', justifyContent: 'flex-start', height:70}}>
                    <View style={{flex:0.2, backgroundColor: 'red'}} />
                    <View style={{flex:0.8, backgroundColor: 'skyblue'}} />
                    <View style={{flex:0.2, backgroundColor: 'steelblue'}} />
                </View>
            </View>
            <View style={{flex: 1, flexDirection: 'row', height: 10}} />
            <View style={{flex: 1, flexDirection: 'row', height: 200, backgroundColor: 'skyblue'}} />
            <View style={{flex: 1, flexDirection: 'row', height: 10}} />
        </ScrollView>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 用另一个使用 flexDirection 样式的视图容器包装我的粘性组件也确实解决了我的问题。谢谢您的帮助! (2认同)