Add margin above top ListView item (and below last) in Android

后端 未结 5 1760
情书的邮戳
情书的邮戳 2020-12-07 08:06

This is a pretty fine question about the layout of items in a ListView in Android.

I have an activity with a title bar at the top and a ListView taking up the rest

5条回答
  •  孤城傲影
    2020-12-07 08:30

    @Gunnar Karlsson's answer is good, but has an issue of cell views being recycled prematurely when completely behind the padding, but not yet fully off the screen. Setting clipToPadding=false is responsible for this and may or may not be fixed in a future version of android.(When using clipToPadding in ListView's the items get recycled prematurely)

    I have a nice simple solution with no side effects:

    1. Add an outer (Linear or Relative) Layout to your cell_design.xml
    2. On this outer Layout add padding (i.e. 10dip) to create a "margin" around the whole cell. (N.B. only padding will work, not margin on the outer layout)
    3. On the ListView set android:dividerHeight="-10dip", the opposite of what is around the cell

    Compared to the other answer, there is no need to set the divider colour. The padding at the topmost and bottommost cells will be present, and the negative divider will prevent double height dividers in between.

提交回复
热议问题