反应原生scrollView高度总是保持静态并且不会改变

Adi*_*ari 14 react-native

我构建反应本机应用程序,我使用scrollView for header与文本列表水平.问题是滚动视图的高度占屏幕的一半大小.即使在宣称它是一种风格之后,它仍然保持原样.

屏幕与scrollView

            <View style={Style.container} >
                {this.props.ExtendedNavigationStore.HeaderTitle ? <BackHeader header={this.props.ExtendedNavigationStore.HeaderTitle} onPressBack={this.goBack} /> : <Header openDrawer={this.openDrawer} />}
                <ScrollView  contentContainerStyle={{flexGrow:1}} style={Style.scrollableView} horizontal showsHorizontalScrollIndicator={false}>
                    {this.renderScrollableHeader()}
                </ScrollView>
                <Routes /> /* stack with dashboard screen */
            </View>
        </Drawer>

    )
}
Run Code Online (Sandbox Code Playgroud)

款式

import {StyleSheet} from 'react-native'
import {calcSize} from '../../utils'


const Styles = StyleSheet.create({
    container : {
        flex:1,  
        backgroundColor:"#e9e7e8"      
    },
    scrollableView:{
        height: calcSize(40),
        backgroundColor: '#000',

    },
    textCategory:{
        fontSize: calcSize(25),
        color:'#fff'
    },
    scrollableButton:{
        flex:1,
        margin:calcSize(30)
    }
})

export default Styles
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

正如您所看到的黑色大小是滚动视图,我希望它很小.

在路由堆栈到仪表板屏幕中,样式:

const Style = StyleSheet.create({
    container: {
        backgroundColor: '#9BC53D',
        flex: 1,
        justifyContent: 'space-around',
        alignItems: 'center'
    },
    text: {
        fontSize: 35,
        color: 'white',
        margin: 10,
        backgroundColor: 'transparent'
    },
    button: {
        width: 100,
        height: 75,
        margin: 20,
        borderWidth: 2,
        borderColor: "#ecebeb",
        justifyContent: "center",
        alignItems: "center",
        borderRadius: 40
    }
})
Run Code Online (Sandbox Code Playgroud)

小智 50

flexGrow:0在ScrollView内部使用样式

<ScrollView style={{ flexGrow:0 }}>
Run Code Online (Sandbox Code Playgroud)

  • 高级答案不在上面。 (3认同)

Rav*_*Raj 47

有与现有的限制ScrollView,其中height不能被直接提供.
把它包裹ScrollView在另一个中View并给它高度View.

喜欢,

  render() {
    return (
      <View style={styles.container}>
        <View style={{height: 80}} >
          <ScrollView
            horizontal
            style={{ backgroundColor: 'blue'}}
          >
            <Text style={{padding: 24}} >title1</Text>
            <Text style={{padding: 24}} >title2</Text>
            <Text style={{padding: 24}} >title3</Text>
            <Text style={{padding: 24}} >title4</Text>
            <Text style={{padding: 24}} >title5</Text>
          </ScrollView>
        </View>
      </View>
    );
  }

const styles = StyleSheet.create({
  container: {
    flex: 1,
    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#ecf0f1',
  },
});
Run Code Online (Sandbox Code Playgroud)

小吃样品: https ://snack.expo.io/HkVDBhJoz

EXTRAS:不同于提供宽度的高度可以ScrollView正常工作

希望这可以帮助.

  • 谢谢,奇怪的是,网上没有多少东西。 (2认同)

Die*_*íaz 5

这对我有用:

<ScrollView contentContainerStyle={{ flexGrow: 1 }}>
  <View style={{ flexGrow: 1 }}>
    ...
  </View>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)