Imageview disappearing from layout
I have a relative layout that I use for several list displays in my application. For separation purposes, I have an image that I populate with a GradientDrawable between my top banner and the actual listview itself. When the activity first starts up, the image displays fine, but as soon as I interact with the ListView, the drawable goes away. If I try to call bringToFront
on the Imageview it doesn't draw at all in the first开发者_Go百科 place.
I have no idea what I'm doing wrong here, please help
Here is my xml layout:
<RelativeLayout android:id="@+id/listLayout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<ImageView android:src="@drawable/icon"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/school_Logo"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="5.0dip" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/school_Logo"
android:textSize="15.0sp"
android:text="List Title"
android:id="@+id/List_Title"
android:layout_alignTop="@+id/school_Logo"
android:layout_alignBottom="@+id/school_Logo"
android:gravity="center_vertical"
android:layout_marginLeft="5.0dip" />
<ImageView android:id="@+id/Image_Divider"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/school_Logo"
android:layout_alignLeft="@+id/school_Logo"
android:layout_marginBottom="10.0dip" />
<Button android:text="Post message"
android:id="@+id/postButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="5.0dip" />
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollingCache="false"
android:cacheColorHint="#00000000"
android:layout_below="@+id/Image_Divider" />
</RelativeLayout>
and here is how I create the drawable for it
int[] colors = {Liveblogging_Main.borderColor,Liveblogging_Main.borderColor,Liveblogging_Main.borderColor};
Drawable dividerDrawable = new GradientDrawable(Orientation.RIGHT_LEFT, colors);
ImageView topDivider = (ImageView) findViewById(R.id.Image_Divider);
topDivider.setImageDrawable(dividerDrawable);
I am not sure why this happens, but I would chose another structure for your layout, one without RelativeLayout. Try something like this:
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical">
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="Horizontal">
<ImageView android:src="@drawable/icon"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/school_Logo"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="5.0dip"
>
</ImageView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/school_Logo"
android:textSize="15.0sp"
android:text="List Title"
android:id="@+id/List_Title"
android:layout_alignTop="@+id/school_Logo"
android:layout_alignBottom="@+id/school_Logo"
android:gravity="center_vertical"
android:layout_marginLeft="5.0dip"
>
</TextView>
<Button android:text="Post message"
android:id="@+id/postButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="5.0dip">
</Button>
</LinearLayout>
<ImageView android:id="@+id/Image_Divider"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/school_Logo"
android:layout_alignLeft="@+id/school_Logo"
android:layout_marginBottom="10.0dip"
>
</ImageView>
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollingCache="false"
android:cacheColorHint="#00000000"
android:layout_below="@+id/Image_Divider"
>
</ListView>
</LinearLayout>
Have you tried setting the height of the Image_Divider , ImageView to something fixed in dp? I don't see where the height for the ImageView should come from (might be wrong).
And BTW, why aren't you defining the GradientDrawable in XML? http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape
Try to set the bounds of the drawable
dividerDrawable.setBounds(0, 0, dividerDrawable.getIntrinsicWidth(),dividerDrawable.getIntrinsicHeight());
I count even find out single problem in your code ,Dan F :)
your code seem correct , i believe there is some other problem which not relevant to this provide code ,
however , here is the best example of list view given by google guys
,i wonder if that can help you a bit
Dev.android.list
精彩评论