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){...},这参数有没有没什么区别吧?

相关推荐
阿狗童鞋6 分钟前
高并发系统设计实战指南
java·spring boot·spring cloud
Sam_Deep_Thinking10 分钟前
CountDownLatch实现原理
java·后端·面试·程序员
脚踏实地,坚持不懈!11 分钟前
从 AOSP 源码角度分析 Android 定位性能问题
android
小狼Solar12 分钟前
SAP MDG 功能范围说明(基于S/4HANA 2025)
java·开发语言
范中勤13 分钟前
Python 与 Java:模块、包、对象、反射核心差异总结
java·python·面试·反射·元类
ab12376815 分钟前
Spring Integration 完整笔记 + 和 spring-integration-mqtt、Paho、EMQX 的关系
java·笔记·spring
小蜗牛狂飙记21 分钟前
java环境一键配置和切换的ui工具
java
逆光的July25 分钟前
可靠消息投递与消费的工程设计:Outbox、幂等、死信兜底
java
悟道子HD30 分钟前
网络安全基本功——MySQL基础
android·mysql·web安全
bro_Java66634 分钟前
《二叉树的基础知识、操作方法与代码示例》
java·数据结构·算法·广度优先