Child of RelativeLayout is not filling the height

2019-02-08 13:17发布

I have a ListView which gets populated with RelativeLayouts. The content of these RelativeLayouts can change and thus change their height respectively. On each RelativeLayout, I have a little vertical bar on the left side of the layout that matches the height of the layout.

It's a similar look to this -> Link

Though for some reason, the View object that I'm using isn't expanding to the height of the layout.

What am I doing wrong?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/post"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/column_post_selector"
android:orientation="horizontal" >

<!-- Status bar (THIS ISN'T FILLING TO PARENTS' HEIGHT) -->
<View
    android:id="@+id/status"
    android:layout_width="3dp"
    android:layout_height="fill_parent"
    android:layout_alignLeft="@id/post"
    android:layout_marginBottom="2dp"
    android:layout_marginTop="2dp"
    android:background="#33B5E5" />

<ImageView
    android:id="@+id/thumbnail"
    android:layout_width="80dip"
    android:layout_height="80dip"
    android:layout_toRightOf="@id/status"
    android:contentDescription="@string/empty"
    android:layout_marginRight="5dp"
    android:scaleType="fitStart" />

<!-- Title -->

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@id/post"
    android:layout_toRightOf="@id/thumbnail"
    android:textColor="#ffffff"
    android:textSize="14sp"
    android:typeface="sans" />
<!-- Rightend Duration -->

<TextView
    android:id="@+id/date"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@id/title"
    android:layout_marginRight="5dip"
    android:gravity="right"
    android:textColor="#10bcc9"
    android:textSize="12sp"
    android:textStyle="bold" />

</RelativeLayout>

7条回答
Summer. ? 凉城
2楼-- · 2019-02-08 14:17

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);
    }
});
查看更多
登录 后发表回答