Question

Good afternoon droids!

I am facing an annoying layout problem to which i have no explanation after hours and hours of investigation... :/. I created a reduced test case which i present here.

I have a simple list item layout.

list item http://www.freeimagehosting.net/4075a.jpg

lis item image link

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"
    android:minHeight="96dp">

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="TextView"
    android:id="@+id/textView1" android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_centerVertical="true" android:layout_centerHorizontal="true" />

<TextView android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="TextView"
    android:id="@+id/textView2" android:layout_alignParentTop="true" />

<ImageView 
    android:layout_width="10dp" 
    android:layout_height="match_parent"
    android:id="@+id/imageView1" 
    android:src="@drawable/errorindicator"
    android:layout_below="@id/textView2" />

</RelativeLayout>

the errorindicator is a red rectangle, which you can see on the image on the left side. The xml looks like that:

    <?xml version="1.0" encoding="utf-8"?>
    <shape shape="rectangle"  xmlns:android="http://schemas.android.com/apk/res/android">
     <solid android:color="@color/ControllingConflictColor" />
     <padding android:left="0dp" android:top="0dp" 
         android:right="0dp" android:bottom="0dp" />
    </shape>

The described list item layout works as expected.

Now i try to fill a list view with list items as described above. The result looks like that:

list view with list items http://www.freeimagehosting.net/4cc4b.jpg

list view image link

The corresponding xml:

    <?xml version="1.0" encoding="utf-8"?>
    <ListView android:id="@+id/listView1" android:layout_width="fill_parent"
        android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- Preview: listitem=@layout/test -->
    </ListView>

As you can see on the image, the red rectangle does not match it's parent in the height anymore as it should. The images are made from the designer, but the same effect is also on the emulator and device. I am developing for API Level 8.

**I'd love if somebody could explain me, why the described layout does not work as expected in a list view. Why does the shape drawable behave different? **

Thank you for your time :)

edit: i have problems embedding the image, i used a link instead, sorry ;( edit: added xml-drawable tag.

edit: My example can be even made much more easier. I included two text views because it has some similarity to my real layout. You can remove the two text views from the example and there still exists the problem, that the shape drawable in the image view does not match the parents height as defined, if the layout is used in a list view.

Was it helpful?

Solution 2

Because i guess my desired behaviour is not possible with the give layout manager, i went another way which fits my personal situation.

I have overriden the onDraw method of the RelativeLayout and paint this red rectangle myself (after the layout process finished and the heights are set)...

OTHER TIPS

Im also finding it hard to get it done using a RelativeLayout. Well try this layout for your row xml. It uses LinearLayout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical">
    <TextView android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="TextView" 
        android:id="@+id/textView2" 
        android:layout_alignParentTop="true" />
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <LinearLayout android:layout_width="10dip" 
            android:layout_height="fill_parent"
            android:background="@drawable/errorindicator"/>       
        <TextView android:layout_width="0dip" 
            android:layout_height="wrap_content" 
            android:text="TextView" android:layout_weight="1.0" 
            android:id="@+id/textView1"
            android:textAppearance="?android:attr/textAppearanceMedium" 
            android:gravity="center"/>
   </LinearLayout>
</LinearLayout>

If you made the asset as a png, you could then 9-patch it and add it as the background of the RelativeLayout

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top