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条回答
乱世女痞
2楼-- · 2019-02-08 13:59

My problem was the the following line

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

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

查看更多
成全新的幸福
3楼-- · 2019-02-08 14:03

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

查看更多
虎瘦雄心在
4楼-- · 2019-02-08 14:03

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">
查看更多
Fickle 薄情
5楼-- · 2019-02-08 14:05

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.

查看更多
霸刀☆藐视天下
6楼-- · 2019-02-08 14:09

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

查看更多
beautiful°
7楼-- · 2019-02-08 14:15

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