How do I avoid double borders between lists?

后端 未结 3 981
一个人的身影
一个人的身影 2021-01-15 16:20

I am using a list view in which I have an xml referencing drawable/list as follows:



        
相关标签:
3条回答
  • 2021-01-15 16:31

    In order to not have double selectors(and in proper positions like also at the top, at the bottom) you have two cases that you need to tackle, the top element(which needs the selector at the top and bottom) plus every other row which needs the selector only at the bottom(the top will be covered by the selector of the previous element). This has to be done in code, in your adapter(if you were using a simple table you could have just made one drawable for the top element and one for the other elements):

    • remove the properties like android:top, android:bottom etc from the layer-list(as you'll be setting those in code)
    • in the getView() method of the adapter retrieve the background of the row view(which will be a LayerDrawable)
    • based on the position you have(mainly 0 vs any oher position) use the LayerDrawable.setLayerInset() method and set the inset for the layer with index 1(if that is your complete drawable): top and bottom for the first element, top(with a value of 0) and bottom for every other position
    0 讨论(0)
  • 2021-01-15 16:43

    The reason for having two lines is, that the background (second layer) lets shine thru the white part on both ends, at the bottom and the top. Therefore every list entry gets a white line on top and on bottom. As list items have a small distance between each other you see two lines.

    Two things you need to do

    • For each item, let only one (top or bottom) shine thru. Therefore you get only one line per item. The drawable XML for an item:

      <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
      <item>
      <shape android:shape="rectangle">
          <solid android:color="@color/white" />
          <corners android:radius="0dp" />
      </shape>
      </item>
      <item android:top="1dp"
            android:left="0dp"
            android:right="0dp"
            android:bottom="0dp">
          <shape android:shape="rectangle">
              <solid android:color="#262626" />
              <corners android:radius="0dp" />
          </shape>
      </item>
      </layer-list>
      
    • For the whole list, you need the drawable that shows the one missing line. The drawable XML for this looks nearly the same. Just bottom and top is exchanged:

      <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
      <item>
      <shape android:shape="rectangle">
          <solid android:color="@color/white" />
          <corners android:radius="0dp" />
      </shape>
      </item>
      <item android:top="0dp"
            android:left="0dp"
            android:right="0dp"
            android:bottom="1dp">
          <shape android:shape="rectangle">
              <solid android:color="#262626" />
              <corners android:radius="0dp" />
          </shape>
      </item>
      </layer-list>
      
    0 讨论(0)
  • 2021-01-15 16:47

    You could play with ListView params android:divider and android:dividerHeight, to set a line separator of the color and height you want.

    0 讨论(0)
提交回复
热议问题