很早之前就遇到过在使用 NestedScrollView 的时候发现底部的 View 总是显示不全, 看起来像是被底部的什 padding 遮挡了一样.
这次是一个 recycleView, 在 list 没有数据的时候总是显示不全, 有数据的时候就正常了. 子类控件高度都设置了 wrap_content, 还是没效果. 以前都是直接在最下面的子控件加一个合适的 layout_marginBottom.. 今天刚好比较闲就决定找到原因, 解决, 也方便以后查阅.
解决方案
NestedScrollView 加一句
Android:fillViewport="true"
完美解决哈哈
原理
出现这个问题感觉应该是 NestedScrollView 的测量方法去找原因
- @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 NestedScrollView.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);
- }
- }
- }
看到这里, mFillViewport, 字面意思填充, 如果是 false 被 return 出去,
- if (!mFillViewport) {
- return;
- }```
看文档说明
- /**
- * When set to true, the scroll view measure its child to make it fill the currently
- * visible area.
- */
- private boolean mFillViewport;
翻译过来
设置为 true 时, 滚动视图将测量其子级以使其填充当前 * 可见区域.
默认值为 false, 果然没有重新测量 , 所以在 xml 添加设置为 true 就解决了
来源: https://blog.csdn.net/qq_39178733/article/details/105391159