react native scrollView 高度始终保持静态不变

新手上路,请多包涵

我构建了反应本机应用程序,并与 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>

    )
}

风格

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

在此处输入图像描述

如您所见,黑色尺寸是滚动视图,我希望它很小。

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

 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
    }
})

原文由 Manspof 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 470
1 个回答

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',
  },
});

零食样本: https ://snack.expo.io/HkVDBhJoz

EXTRAS:与高度不同,为 ScrollView 提供宽度将正常工作

希望这可以帮助。

原文由 Ravi Raj 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题