简体   繁体   中英

Why ListView doesnot show divider

For this perticular case i am using default listview layout and I am extending my activity with ListFragment . I am having a problem to show divider in listview. I am using the follwing xml file for my listview row formate.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center_vertical"
android:background="@color/windowBackground"
android:padding="2dp" >

       <RelativeLayout android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true" >

            <TextView android:id="@+id/tvtitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="@android:style/TextAppearance.Medium" />

            <TextView android:id="@+id/tvdescription"
                android:layout_below="@id/tvtitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="@android:style/TextAppearance.Small" />

      </RelativeLayout>

      <!-- This is Supposed to be the divider -->
      <RelativeLayout
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:background="@drawable/shape" >
      </RelativeLayout>

</RelativeLayout>

The secondlast RelativeLayout which is using the shape.xml file is supposed to be the divider. Shape.xml file is as follows

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="rectangle" >

    <corners android:radius="2px" />
    <stroke
        android:width="2px"
        android:color="@color/colorAccent" />
    <solid android:color="@color/colorPrimary"/>
</shape>

The list data output is fine. I can see data in both TextViews but it does not show the divider which is RelativeLayout with a background @drawable/shape

Right you could put a divider inside your layout row just like you did but you have to correct it

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center_vertical"
android:background="@color/windowBackground"
android:padding="2dp" >

       <RelativeLayout 
           android:id="@+id/row_id"
           android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true" >

            <TextView android:id="@+id/tvtitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="@android:style/TextAppearance.Medium" />

            <TextView android:id="@+id/tvdescription"
                android:layout_below="@id/tvtitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="@android:style/TextAppearance.Small" />

      </RelativeLayout>

      <!-- This is Supposed to be the divider -->
      <RelativeLayout
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_below="@id/row_id"
           android:layout_marginTop="5dp"
           android:layout_marginBottom="5dp"
           android:background="@color/yourColor" >
      </RelativeLayout>

</RelativeLayout>

or more simple way is to get your listView from the divide and put a divider to it

getListView().setDividerHeight(heightOfDivider);
getListView().setDivider(yourDrawable);

if any trouble leave a comment

Good luck !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM