给父容器设置flex: 1后,子容器ScrollView设置height不生效

import { View, Text, ScrollView } from 'react-native';
import React from 'react';

export default function Index() {
  return (
  <View style={{flex: 1}}>
    <ScrollView style={{height: 400}}>
      <View style={{backgroundColor: 'blue', height: 1000}}>
        <Text style={{color: 'white'}}>你好</Text>
      </View>
    </ScrollView>

    <Text style={{backgroundColor: 'red', color: 'white'}}>哈哈</Text>
  </View>
  )
}

原因

react native 0.73版本为NativeDirectionalScrollView设置了flexGrow: 1。由于‘哈哈’text并未设置flexGrow属性,所以NativeDirectionalScrollView会独占剩余空间,表现在屏幕上就如上图所示。

关于flexGrow属性原理可参考【css】flex弹性布局中的flex:1 到底是什么意思?_flex:1-优快云博客

解决方案

在ScrollView外层包一层view

import { View, Text, ScrollView } from 'react-native';
import React from 'react';

export default function Index() {
  return (
  <View style={{flex: 1}}>
    <View>
      <ScrollView style={{height: 400}}>
        <View style={{backgroundColor: 'blue', height: 1000}}>
          <Text style={{color: 'white'}}>你好</Text>
        </View>
      </ScrollView>
    </View>
    <Text style={{backgroundColor: 'red', color: 'white'}}>哈哈</Text>
  </View>
  )
}

### 微信小程序中实现可滚动视图区域 在微信小程序开发过程中,`<scroll-view>` 组件用于创建一个可以滚动的内容区。此组件支持水平和垂直方向上的滚动,并允许开发者自定义其样式和其他属性。 #### 创建基本的 `scroll-view` 为了使页面内的某些部分能够滚动,需使用 `<scroll-view>` 标签包裹住想要设置成可滚动区域的内容: ```html <scroll-view class="example-scroll-view" scroll-y> <!-- 这里放置要滚动显示的内容 --> </scroll-view> ``` 上述代码片段设置了竖直方向上 (`scroll-y`) 的滚动功能;如果希望启用水平滚动,则应将 `scroll-x` 属性设为 true 或者不写默认即为 false[^1]。 #### 配置 `scroll-view` 参数 除了指定滚动的方向外,还可以通过配置其他参数来增强用户体验,比如调整滚动条外观、控制初始位置以及响应事件等。下面是一些常用的属性列表及其说明: - **upper-threshold**: 距顶部/左边多远时触发滚动到底部/右边事件,默认值为50px; - **lower-threshold**: 同 upper-threshold, 不过是指底部/右边距离; - **bindscrolltoupper/bindingrolltolower**: 当达到设定阈值时触发相应函数; - **scroll-top / scroll-left**: 设置当前滚动的位置(仅限 js 中动态修改有效),单位 px; - **scroll-with-animation**: 是否开启平滑过渡效果,在改变 scrollTop 或 scrollLeft 值的时候生效; - **enable-flex**: 开启 flexbox 支持,使得元素可以根据父容器大小自动布局; - **style & class**: 自定义内联样式或 CSS 类名以进一步美化界面. #### 移除滚动条视觉呈现 对于一些特定的应用场景来说,可能并不期望看到实际存在的滚动指示器。针对这种情况,可以通过如下方式隐藏掉 iOS 和 Android 设备上的原生滚动: ```css /* 安卓 */ .example-scroll-view { -ms-overflow-style:none; /* IE and Edge */ scrollbar-width: none; /* Firefox */ } /* 苹果 */ .example-scroll-view::-webkit-scrollbar{ display: none; } ``` 值得注意的是,虽然上面的方法可以在大多数情况下正常工作,但在不同版本的操作系统之间可能会存在差异。因此建议测试多个平台确保最佳表现。 #### 示例代码展示 这里给出一段完整的 HTML 结构加上对应的 WXSS 文件内容作为例,展示了如何构建并优化带有滚动特性的视窗部件: ##### WXML (HTML) ```xml <!-- index.wxml --> <view class="container"> <text>向上拉动加载更多...</text> <scroll-view id="scrollView" class="content-area" scroll-y="{{true}}" bindscrolltoupper="onScrollToUpper" bindscrolltolower="onScrollToLower" lower-threshold="80" > {# 动态渲染的数据项 } </scroll-view> <button type="primary" bindtap="loadMore">点击加载更多数据</button> </view> ``` ##### WXSS (CSS) ```css /* index.wxss */ .container { height: 100vh; padding-bottom: 60rpx; } .content-area { background-color:#f7f7f7; margin-top:20rpx; /* 隐藏滚动条 */ -ms-overflow-style:none; scrollbar-width: none; ::-webkit-scrollbar{display:none;} } ``` 以上就是关于如何利用 WeChat Mini Program Framework 构建具有滚动能力的小程序页面的相关介绍和技术要点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值