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

相关推荐
广州灵眸科技有限公司1 分钟前
瑞芯微RV1126B开发板(EASY-EAI-PI2) INI文件操作
java·前端·javascript·网络·人工智能
库克克13 分钟前
【C++ 】内联函数
java·开发语言·c++
zh_xuan19 分钟前
java 虚拟线程
java·开发语言·虚拟线程
心中有国也有家21 分钟前
AtomGit Flutter 鸿蒙客户端:ModalBottomSheet 实战
android·javascript·学习·flutter·华为·harmonyos
jason成都39 分钟前
RTKLIB Java 移植踩坑复盘:卡尔曼滤波核心逻辑修复与滑坡场景实测验证
java·开发语言
老毛肚1 小时前
juc线程通信
java·开发语言·jvm
花颜yyds2 小时前
Java 开发基础速查手册
java
YSoup2 小时前
Android Stuidio中下载TRAE插件依赖的JCEF后打不开
android
没钥匙的锁12 小时前
05-Java面向对象构造器与封装
java·开发语言
2401_894915532 小时前
Geo搜索优化排名源码部署搭建全流程详解
android·开发语言·flask·php·精选