How to give empty space between rows of listview?

后端 未结 2 621
情歌与酒
情歌与酒 2021-01-22 10:13

In my application I have a requirement of listview with empty spaces between rows of list.So that I can give background to each row and it will look something like blocks of row

相关标签:
2条回答
  • 2021-01-22 10:49

    I had the same problem except I needed to set the divider programatically because the view was dynamically generated. I'm posting this answer because it was my first google hit for future reference.

    You need a custom Drawable.
    Create a new file with the following code:
    drawable/list_divider.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" >
        <size
            android:height="16dp"/>
        <solid android:color="@android:color/transparent"/>
    </shape>
    

    Inside your java code you need to get a reference to your ListView and set the divider like this:

    listView.setDivider(getResources().getDrawable(R.drawable.list_divider));
    

    The result is exactly the same.

    0 讨论(0)
  • 2021-01-22 11:08

    You can use android:divider and android:dividerHeight to customize the spacing between rows.

    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">
    
      <ListView 
        android:id="@+id/android:list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:divider="#FF0000"
        android:dividerHeight="4px"/>
    
    </LinearLayout>
    
    0 讨论(0)
提交回复
热议问题