Child of RelativeLayout is not filling the height of the RelativeLayout

后端 未结 8 1797
终归单人心
终归单人心 2021-01-02 01:04

I have a ListView which gets populated with RelativeLayouts. The content of these RelativeLayouts can change and thus change their height respectively. On each RelativeLayou

8条回答
  •  无人及你
    2021-01-02 01:14

    I had a similar problem with a RelativeLayout inside a ScrollView. According to the documentation there is a bug in RelativeLayout (up to version 17) that cause mistakes in measured height if it is added inside scrolling container (like ListView).

    So to solve the problem you should calculate height programmatically.

    You could make your extended class and override the onMeasure method or add a OnGlobalLayoutListener to the parent view.

    I choose the second way. Here is my solution:

    Example

    findViewById(R.id.scroll_view).getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            float topElementPosition = findViewById(R.id.top_element).getY();
            float bottomElementPosition = findViewById(R.id.bottom_element).getY();
            int bottomElementHeight = findViewById(R.id.bottom_element).getMeasuredHeight();
            int height = (int)(bottomElementPosition-topElementPosition+bottomElementHeight);
            findViewById(R.id.view).setMinimumHeight(height);
            findViewById(R.id.scroll_view).getViewTreeObserver().removeGlobalOnLayoutListener(this);
        }
    });
    

提交回复
热议问题