Search code examples
androidviewinflatedivider

Android inflated view is empty


I am trying to add a divider between entries in a vertical LinearLayout, to emulate the appearance of a ListView. (I can't just use a ListView in this particular situation.)

This is what I have in list_divider.xml:

<?xml version="1.0" encoding="utf-8"?>
<View
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:background="@color/panel_border"
  android:layout_width="fill_parent"
  android:layout_height="@dimen/border_width"
 />

And here's the code which attempts to inflate this divider before every element except the first in the list:

LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < categories.size(); i++ ) {

    if (i > 0)
        items.addView(vi.inflate(R.layout.list_divider, null));    
        // these dividers never appear

    // but these main entries always appear just fine    
    items.addView(ad.getView(i, null, null));
}

The main list items appear correctly, but the dividers are invisible.

The dividers do appear if I change them to be a TextView rather than a plain View:

<?xml version="1.0" encoding="utf-8"?>
<TextView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:background="@color/panel_border"
  android:layout_width="fill_parent"
  android:layout_height="@dimen/border_width"
  android:text="---"
 />

I have tried setting explicit pixel values for the width and height, as well as using the border_width definition and fill_parent options. It makes no difference.

Is there something special about a plain old View which makes it not appear?


Solution

  • I eventually worked around it by wrapping my divider View inside a FrameLayout. It seems that having a single empty View with no content just doesn't work.

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content">
    
      <View
        android:background="@color/panel_border"
        android:layout_width="fill_parent"
        android:layout_height="@dimen/border_width" />
    
    </FrameLayout>