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

相关推荐
鞋尖的灰尘12 分钟前
springboot-事务
java·后端
银迢迢19 分钟前
SpringCloud微服务技术自用笔记
java·spring cloud·微服务·gateway·sentinel
用户03321266636725 分钟前
Java 将 CSV 转换为 Excel:告别繁琐,拥抱高效数据处理
java·excel
这周也會开心31 分钟前
Java-多态
java·开发语言
渣哥40 分钟前
揭秘!Java反射机制到底是什么?原来应用场景这么广!
java
叫我阿柒啊43 分钟前
Java全栈开发实战:从Spring Boot到Vue3的项目实践
java·spring boot·微服务·性能优化·vue3·全栈开发
CPU NULL1 小时前
Spring拦截器中@Resource注入为null的问题
java·人工智能·后端·spring
Digitally1 小时前
如何轻松永久删除 Android 手机上的短信
android·智能手机
藤椒鱼不爱编程1 小时前
面向对象_抽象类与接口
java
MacroZheng1 小时前
告别Swagger UI!一款更适合Spring Boot的API文档新选择!
java·spring boot·后端