简体   繁体   中英

How to add Kivy list view padding between rows

Is it possible to apply vertical padding between rows on a Kivy list view? I have tried modifying the kv file padding on both the list view and the gridlayout to no avail.

+++++++
+ Row +  
+++++++
+ Pad +
+++++++
+ Row +
+++++++

The following will add padding between the ListView items by using a ListAdapter and list_items_arg_converter where you can modify the look of your ListItems. It does not change the padding of the underlying GridLayout but just changes the height of the items.

Hope this helps,
Cheers

[Kivy ] v1.9.1
[Python ] v2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 20:32:19) [MSC v.1500 32 bit (Intel)]

from kivy.uix.boxlayout import BoxLayout
from kivy.adapters.dictadapter import ListAdapter
from kivy.uix.button import Button
from kivy.uix.listview import ListView, ListItemButton
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.app import App

from random import randint


class MyApp(App):
    items = 20
    pad = 20
    space = 20
    text_border = 10

    def build(self):
        mainlayout = BoxLayout(orientation = 'vertical')
        self.list_adapter = ListAdapter(data = ["Item #{0}".format(i) for i in range(self.items)], cls = ListItemButton,
                                        args_converter=self.list_item_args_converter, sorted_keys = [])
        self.list_view = ListView(adapter=self.list_adapter, size_hint_y=.3)
        self.list_view.container.padding = self.pad
        self.list_view.container.spacing = self.space
        mainlayout.add_widget(self.list_view)
        return mainlayout

    def list_item_args_converter(self,col,obj):
        return {
            'text': obj,
            'size_hint_y': .3,
            'text_size' : (self.root.width-2*self.pad-2*self.text_border , None),
            'height': 32,
            'halign': 'left',
            'valign': 'middle',
            'deselected_color':[.5,.5,.5,1],
            'selected_color':[1.,1.,1.,1]
        }


if __name__ == '__main__':
    MyApp().run()

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