Child of RelativeLayout is not filling the height of the RelativeLayout

﹥>﹥吖頭↗ 提交于 2019-11-30 12:37:57

You'll want to add layout_alignParentTop="true" and layout_alignParentBottom="true" to status - this will ensure that the view touches both the top and bottom of post's RelativeLayout.

RWIL

I had a similar issue inside the cells of my RecyclerView which had a RelativeLayout as root element.

A workaround for me was wrapping an extra LinearLayout around the root RelativeLayout element.

So you card looks like this:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

        <RelativeLayout 
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
            <View
                android:id="@+id/view_with_same_height_as_parent"
                android:layout_width="10dp"
                android:layout_height="fill_parent"
                android:layout_alignParentBottom="true"
                android:layout_alignParentTop="true"/>
...


    </RelativeLayout>
</LinearLayout>

Source: https://stackoverflow.com/a/28799251/3708094

If your RelativeLayout is inside a ScrollView, then on the ScrollView you need to add the fillViewPort param and set it to true

  <ScrollView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:fillViewport="true">

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:

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);
    }
});

Had the same pesky problem which by the way makes no sense. What worked for me in the end was changing the android:layout_height in RelativeLayout tag to fill_parent like this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
  ...

hope this helps

I fixed by manually set layout during onLayout for invalid view:

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);

    int rootMarginRight = ((MarginLayoutParams)root.getLayoutParams()).rightMargin;
    int paddingRight = root.getPaddingRight() + rootMarginRight;

    //invalid view
    actionsContainer.layout(
            right - actions.getWidth() - paddingRight,
            0,
            right - paddingRight,
            getHeight()
    );
}

If you know the exact height you want to set, you can set Height or MinHeight for this component (View). In your case, you can set with: android:minHeight="76dp", or you can set it programmatically using LayoutParams. Hope it helps!

Gerardo Guijarro

My problem was the the following line

android:fitsSystemWindows="true" <-- In the parent view

was removed. Having added it back, it works fine now.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!