【发布时间】:2021-07-14 06:48:33
【问题描述】:
我在将复杂的小部件添加到 recycleview 时遇到问题,主要是尺寸问题。 Recycleview 似乎对每一行中的内容都有大小限制。
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.recycleview import RecycleView
from kivy.uix.popup import Popup
from kivymd.uix.card import MDCard
Builder.load_string('''
#:kivy 1.10.0
<RecycleViewRow>:
size_hint_y: None
size: "180dp", "280dp"
pos_hint: {"center_x": .5, "center_y": .5}
orientation: "vertical"
padding: 10
border_radius: 20
radius: [15]
elevation:0
MDLabel:
text: root.text
theme_text_color: "Custom"
font_style: "H6"
font_size: "20sp"
text_color: (0/255.0,141/255.0,155/255.0,255/255.0)
MDBoxLayout:
orientation: "vertical"
padding: 20, 0
MDBoxLayout:
MDLabel:
text: "State: "
theme_text_color: "Custom"
font_style: "Subtitle2"
font_size: "14sp"
text_color: (0/255.0,141/255.0,155/255.0,255/255.0)
MDLabel:
text: "state name"
theme_text_color: "Custom"
font_style: "Body2"
font_size: "12sp"
text_color: (0/255.0,141/255.0,155/255.0,255/255.0)
MDBoxLayout:
MDLabel:
text: "City: "
theme_text_color: "Custom"
font_style: "Subtitle2"
font_size: "14sp"
text_color: (0/255.0,141/255.0,155/255.0,255/255.0)
MDLabel:
text: "city name"
theme_text_color: "Custom"
font_style: "Body2"
font_size: "12sp"
text_color: (0/255.0,141/255.0,155/255.0,255/255.0)
MDBoxLayout:
MDLabel:
text: "Locality: "
theme_text_color: "Custom"
font_style: "Subtitle2"
font_size: "14sp"
text_color: (0/255.0,141/255.0,155/255.0,255/255.0)
MDLabel:
text: "locality name"
theme_text_color: "Custom"
font_style: "Body2"
font_size: "12sp"
text_color: (0/255.0,141/255.0,155/255.0,255/255.0)
MDBoxLayout:
MDLabel:
text: "Pincode: "
theme_text_color: "Custom"
font_style: "Subtitle2"
font_size: "14sp"
text_color: (0/255.0,141/255.0,155/255.0,255/255.0)
MDLabel:
text: "pincode"
theme_text_color: "Custom"
font_style: "Body2"
font_size: "12sp"
text_color: (0/255.0,141/255.0,155/255.0,255/255.0)
MDBoxLayout:
MDLabel:
text: "Last Verified: "
theme_text_color: "Error"
font_style: "Subtitle2"
MDLabel:
text: "verification time"
theme_text_color: "Error"
font_style: "Subtitle2"
font_size: "14sp"
MDRaisedButton:
text: "Get Details"
md_bg_color: 0/255.0,141/255.0,155/255.0,255/255.0
<MainScreen>:
viewclass: 'RecycleViewRow'
RecycleGridLayout:
cols:1
default_size: None, dp(56)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
''')
class RecycleViewRow(MDCard):
text = StringProperty()
class MainScreen(RecycleView):
def __init__(self, **kwargs):
super(MainScreen, self).__init__(**kwargs)
self.data = [{'text': "Button " + str(x), 'id': str(x)} for x in range(100)]
class TestApp(MDApp):
title = "RecycleView Direct Test"
def build(self):
return MainScreen()
if __name__ == "__main__":
TestApp().run()
我已经用普通的滚动视图完成了这项工作,但是生成所有框及其内容需要很长时间,因为我需要生成其中的许多。
就像我在下面所说的那样,我想出了这部分,但知道我无法将它与中心对齐。就像如果我减小宽度,我需要这样做,它会留在左边。我什至尝试将recycleview 放到anchorlayout 中,但没有得到锚定。
【问题讨论】: