Search code examples
androidandroid-imageviewandroid-linearlayout

Image doesn't show up when it's last in LinearLayout


This has puzzled me for two days now. I have a LinearLayout like the following:

        <LinearLayout
                android:background="@drawable/rounded_rect_wht_bg"
                android:layout_height="wrap_content"
                android:layout_width="fill_parent"
                android:orientation="horizontal"
                android:layout_margin="5dp"
                android:minHeight="30dp"
                android:padding="5dp"
                android:visibility="gone">
            <ImageView
                    android:id="@+id/timage"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:layout_marginRight="10dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginBottom="5dp"
                    android:scaleType="fitXY"
                    />
            <TextView
                    android:id="@+id/tname"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_marginRight="5dp"
                    android:textColor="@android:color/black"
                    android:textSize="13sp"
                    android:textStyle="bold"/>
            <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="5dp"
                    android:src="@drawable/ticon" />
        </LinearLayout>

The last ImageView (ticon) doesn't showup. I only see timage and tname. But, if I switched it:

<LinearLayout>
        <ImageView android:src="@drawable/ticon" />
        <ImageView android:id="@+id/timage" />
        <TextView android:id="@+id/tname" />
</LinearLayout>

It's there. What is going on here?


Solution

  • Since you're defining the layout_height of your TextView as "fill_parent", it will take the remaining space. That's why it shows properly if it's declared last: the images use only the space they need (layout_height="wrap_content").

    Depending on what you want to do, you can either try setting the layout_height of the TextView to "wrap_content" or, if you want the TextView to fill up the space between the images, I would suggest you to use RelativeLayout:

    <RelativeLayout
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         ...>
         <ImageView
              android:id="@+id/timage"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              ...
              android:layout_alignParentTop="true" />
         <ImageView
              android:id="@+id/ticon"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              ...
              android:layout_alignParentBottom="true" />
         <TextView
              android:id="@+id/tname"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              ...
              android:layout_below="@+id/timage"
              android:layout_above="@+id/ticon" />
    </RelativeLayout>