Android: Total height of ScrollView

前端 未结 2 2038
旧巷少年郎
旧巷少年郎 2020-11-30 04:23

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()

相关标签:
2条回答
  • 2020-11-30 05:05

    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

    private int getScrollRange() {
        int scrollRange = 0;
        if (getChildCount() > 0) {
            View child = getChildAt(0);
            scrollRange = Math.max(0,
                    child.getHeight() - (getHeight() - mPaddingBottom - mPaddingTop));
        }
        return scrollRange;
    }
    
    0 讨论(0)
  • A ScrollView always has 1 child. All you need to do is get the height of the child to determine the total height:

    int totalHeight = scrollView.getChildAt(0).getHeight();
    
    0 讨论(0)
提交回复
热议问题