Android:ScrollView 的总高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3609297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:58:23  来源:igfitidea点击:

Android: Total height of ScrollView

androidscrollscrollview

提问by Erik

I have a custom ScrollView (extended android.widget.ScrollView) which I use in my layout. I want to measure the total height of the contents of this scrollview. getHeight() and getMeasuredHeight() don't give me correct values (too high numbers).

我有一个自定义的 ScrollView(扩展的 android.widget.ScrollView),我在我的布局中使用了它。我想测量此滚动视图内容的总高度。getHeight() 和 getMeasuredHeight() 没有给我正确的值(数字太高)。

Background information: I want to determine how far the user has scrolled. I use onScrollChanged to get the X value, but I need to know a percentage so I'll need the total scrollbar height.

背景信息:我想确定用户滚动了多远。我使用 onScrollChanged 来获取 X 值,但我需要知道一个百分比,所以我需要滚动条的总高度。

Thanks a lot! Erik

非常感谢!埃里克

回答by satur9nine

A ScrollView always has 1 child. All you need to do is get the height of the child to determine the total height:

一个 ScrollView 总是有 1 个孩子。您需要做的就是获取孩子的身高以确定总身高:

int totalHeight = scrollView.getChildAt(0).getHeight();

回答by Nino van Hooff

See the source of ScrollView. Unfortunately, this method is private, but you could copy it into your own code. Note that other answers don't take padding into account

查看ScrollView的源码。不幸的是,此方法是私有的,但您可以将其复制到您自己的代码中。请注意,其他答案不考虑填充

private int getScrollRange() {
    int scrollRange = 0;
    if (getChildCount() > 0) {
        View child = getChildAt(0);
        scrollRange = Math.max(0,
                child.getHeight() - (getHeight() - mPaddingBottom - mPaddingTop));
    }
    return scrollRange;
}