NestedScrollView动态设置子View高度不生效

java 复制代码
 @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        if (!mFillViewport) {
            return;
        }

        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (heightMode == MeasureSpec.UNSPECIFIED) {
            return;
        }

        if (getChildCount() > 0) {
            View child = getChildAt(0);
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();

            int childSize = child.getMeasuredHeight();
            int parentSpace = getMeasuredHeight()
                    - getPaddingTop()
                    - getPaddingBottom()
                    - lp.topMargin
                    - lp.bottomMargin;

            if (childSize < parentSpace) {
                int childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec,
                        getPaddingLeft() + getPaddingRight() + lp.leftMargin + lp.rightMargin,
                        lp.width);
                int childHeightMeasureSpec =
                        MeasureSpec.makeMeasureSpec(parentSpace, MeasureSpec.EXACTLY);
                child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
            }
        }
    }

当child高度小于parent的可用高度时,子view的高度会重置为parentSpace!

如果要修改其子view高度,也要同时修改NestedScrollView的高度。

PS:

java 复制代码
    /**
     * When set to true, the scroll view measure its child to make it fill the currently
     * visible area.
     */
    private boolean mFillViewport;
  1. 看注释,这个参数的意思就是子view填满父view,但这里都return出去了,子view高度是怎么测量的?

  2. 都有 if (childSize < parentSpace){...},这参数有没有没什么区别吧?

相关推荐
Stick_ZYZ1 分钟前
从项目启动到 Milvus 向量检索,我把 RAG 项目链路又打通了一层
java·人工智能·经验分享·ai·milvus
小镇敲码人3 分钟前
MySQL事务介绍
android·数据库·mysql·adb
码上有光4 分钟前
c++:多态
java·jvm·c++·多态·多态原理
Lumbrologist4 分钟前
【C++】零基础入门 · 第 18 节:互斥锁与线程同步
java·开发语言·c++
awu的Android笔记5 分钟前
IP/TCP/UDP 解析器:一次搞懂网络包结构
android
2601_957418806 分钟前
Android相机有线连接全链路优化:PTP/MTP协议栈实现与商业级性能调优
android·数码相机·智能手机·架构
plainGeekDev8 分钟前
Fragment 手动跳转 → Navigation 组件
android·java·kotlin
plainGeekDev10 分钟前
XML 主题 → Compose Material3 主题
android·java·kotlin
武子康13 分钟前
Java-14 深入浅出 MyBatis 插件机制深度解析:四大对象拦截与动态代理原理
java·后端
__Witheart__17 分钟前
HW-T3568 安卓固件编译指南
android