0

I faced strange ViewStub behavior in RelativeLayout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <ViewStub
        android:id="@+id/stub"
        android:inflatedId="@+id/stub"
        android:layout="@layout/some_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"/>

    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:text="Some text"
        android:layout_height="wrap_content"/>

    <TextView
        android:layout_above="@id/stub"
        android:layout_width="match_parent"
        android:text="Some text2"
        android:layout_height="wrap_content"/>

</RelativeLayout>

When inflating layout above it looks like viewstub is aligned to parent top but not bottom. I also have tried to set ViewStub height and width in fixed values (dp or px) but got same result. So is it some kind of Android bug or i missed something with ViewStub?

For example if i'll replace ViewStub with simple View and same RelativeLayout proprties all views infleted in proper way:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="vertical"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent">

        <View
            android:id="@+id/stub"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_alignParentBottom="true"/>

        <TextView
            android:id="@+id/text1"
            android:layout_width="match_parent"
            android:text="Some text"
            android:layout_height="wrap_content"/>

        <TextView
            android:layout_above="@id/stub"
            android:layout_width="match_parent"
            android:text="Some text2"
            android:layout_height="wrap_content"/>

    </RelativeLayout> 

Edit: I'm not calling inflate on ViewStub. In case inflate method called on ViewStub all works perfect.

1 Answer 1

2

Sorry bro, I just misunderstood what you mean.

If you see the Offical Document , you may see this message "A ViewStub is an invisible, zero-sized View". And let us see some source code in ViewStub:

private void initialize(Context context) {
    mContext = context;
    setVisibility(GONE);
    setWillNotDraw(true);
}

When the ViewStub initialize, it set to GONE, That's why your android:layout_alignParentBottom="true" is invalid in RelativeLayout.

You can try to use a "Simple View" instead of ViewStub, and set that view visibility to GONE. android:layout_alignParentBottom="true" is also invalid.

So, I don't think this is a Bug!

Edit: I find a similar question:Issue with RelativeLayout when View visibility is View.GONE

And you can add follow code in you "Some text2" TextView, and everything solve! android:layout_alignWithParentIfMissing="true"

2
  • Yes you'll get this result if ViewStub will be inflated. But in my case i need to inflate it only in rare cases.
    – Ihor DIM
    May 12, 2015 at 16:57
  • Hey bro, I just misunderstood and update the Answer.
    – codezjx
    May 12, 2015 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.