Search code examples
androidandroid-recyclerviewandroid-relativelayout

android:layout_alignParentBottom not working in runtime


I am using android:layout_alignParentBottom for the arrow down ImageView inside RelativeLayout for each item of RecyleView.
But it only work perfect in Preview mode, when I run app it doesn't work. I don't know why this happened.
I have explained more about my problem in the screenshot

In Preview
enter image description here
In runtime
enter image description here

 <android.support.v7.widget.CardView
        android:id="@+id/cv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:id="@+id/item_layoutStart"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#E8F5E9"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:layout_gravity="center_vertical|right"
                    android:clickable="true"
                    android:scaleType="fitCenter"
                    android:src="@mipmap/ic_launcher" />

                <ImageView
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:layout_gravity="center_vertical|right"
                    android:clickable="true"
                    android:scaleType="fitCenter"
                    android:src="@mipmap/ic_launcher" />
                <ImageView
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:layout_gravity="center_vertical|right"
                    android:clickable="true"
                    android:scaleType="fitCenter"
                    android:src="@mipmap/ic_launcher" />
                <TextView
                    android:id="@+id/item_tvRepeatTime"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="10" />
            </LinearLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#ff0"
                android:layout_alignTop="@id/item_layoutStart"
                android:layout_alignBottom="@id/item_layoutStart"
                android:orientation="vertical"
                android:layout_toRightOf="@id/item_layoutStart">
                <TextView
                    android:id="@+id/item_tvTitle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="aa aa aaaaaa aa aa aaaaaa aa aa aaaaaa " />
                <TextView
                    android:id="@+id/item_tvResult"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/item_tvTitle"
                    android:text="aa aa aaaaaa aa aa aaaaaa aa aa aaaaaa" />
                <ImageView android:id="@+id/item_ivExpand"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentBottom="true"
                    android:padding="3dp"
                    android:src="@drawable/selector_arrow_down"/>
            </RelativeLayout>
        </RelativeLayout>
    </android.support.v7.widget.CardView>

UPDATE If I fix the height of my RelativeLayout by specific value, the android:layout_alignParentBottom work

 <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="200dp"
 > 

but in my case, I want RelativeLayout height equals LinearLayout

<RelativeLayout
                android:layout_alignTop="@id/item_layoutStart"
                android:layout_alignBottom="@id/item_layoutStart"
               >

android:layout_alignParentBottom not work

Any help would be great aprreciated


Solution

  • Though this solution apparently messes with the way the layout looks in the editor, it solves the problem of the runtime layout.

    First, change the outer RelativeLayout to a LinearLayout. Then, on the inner RelativeLayout, remove the layout_alignTop and layout_alignBottom attributes altogether, and set the layout_height to match_parent.

    As you can see in the posted screenshots, that ImageView was being laid out as though its parent RelativeLayout's height was wrapping its child Views, as per its layout_height setting. However, after the RelativeLayout's child Views were laid out, its layout_alignTop and layout_alignBottom attributes were being applied, and it was resized to match the LinearLayout's height, but that ImageView just stayed where it was.

    By using a LinearLayout for the outer ViewGroup, and setting the inner RelativeLayout's height to match_parent, the RelativeLayout had a definite height applied before it laid out its child Views, and the ImageView's layout_alignParentRight and layout_alignParentBottom attributes landed it in the right spot.