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.

Ihor DIM
  • 689
  • 1
  • 10
  • 22

1 Answers1

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"

Community
  • 1
  • 1
codezjx
  • 9,012
  • 5
  • 47
  • 57