根据 children 的大小动态调整小部件的大小
Dynamically sizing widgets depending on size of children
我已经看到 this question and answer 这与我想要实现的目标非常接近。
我希望有一个 ScrollView,它可以在我的 MythTV 机顶盒上显示即将录制的内容。有一个显示日期的 header 小部件(固定大小)和一个用于记录详细信息的单独小部件 (BoxLayout)(显示在标签 child 小部件中)。
我需要根据文本的高度调整 BoxLayout 的大小。这里的问题是,文本最多的标签并不总是相同的,因此最大高度需要考虑所有 children.
我的 KV 语言文件中有这个:
<MythRecordingHeader>
text: root.rec_date
height: 25
size_hint_y: None
halign: "left"
<MythRecording>
orientation: "horizontal"
#height: 60
height: max(mrec_desc.texture_size[1], mrec_time.texture_size[1], mrec_title.texture_size[1])
size_hint_y: None
BGLabel:
id: mrec_time
size_hint: 0.2, None
text_size: self.width, None
height: max(self.texture_size[1], mrec_title.texture_size[1], mrec_desc.texture_size[1])
#size_hint_x: 0.2
text: root.rec["time"]
bgcolour: root.bg
BGLabel:
id: mrec_title
size_hint: 0.3, None
text_size: self.width, None
height: max(self.texture_size[1], mrec_time.texture_size[1], mrec_desc.texture_size[1])
#size_hint_x: 0.3
text: "{}\n{}".format(root.rec["title"], root.rec["subtitle"])
halign: "left"
bgcolour: root.bg
BGLabel:
id: mrec_desc
text: root.rec["desc"]
size_hint: 0.5, None
text_size: self.width, None
height: max(self.texture_size[1], mrec_time.texture_size[1], mrec_title.texture_size[1])
halign: "left"
bgcolour: root.bg
(BGLabel
是一个自定义小部件,它只是通过 bgcolour
属性 向标签添加纯色背景色)。
在我的 python 文件中:
class MythRecording(BoxLayout):
rec = DictProperty({})
bg = ListProperty([0.1, 0.15, 0.15, 1])
def __init__(self, **kwargs):
super(MythRecording, self).__init__(**kwargs)
self.rec = kwargs["rec"]
class MythRecordingHeader(BGLabel):
rec_date = StringProperty("")
def __init__(self, **kwargs):
super(MythRecordingHeader, self).__init__(**kwargs)
self.bgcolour = [0.1, 0.1, 0.4, 1]
self.rec_date = kwargs["rec_date"]
这几乎符合我的预期 - BoxLayouts 的高度确实有所不同,并且总体上显示了所有文本,但有一两次有一些剪裁。我不清楚这是否是由于水平对齐造成的。
我目前将所有这些 children 放在一个 BoxLayout 中。
但是,我真正的问题是我需要将它放在 ScrollView 中(因此我提到了 link)。为了让滚动正常工作,我想我需要设置 BoxLayout 的高度,但我真的很难计算它的高度。
遍历 BoxLayout 的 children 并查看 height
属性 只显示 MythRecordings
的高度为 0。
有没有办法获取所有 children 的总高度并设置 BoxLayout 的高度以便我的滚动视图正常工作?
用 GridLayout 替换 BoxLayout,并设置:
size: self.minimum_size
GridLayout 的 minimum_size 属性 自动跟踪其子项的大小。
我已经看到 this question and answer 这与我想要实现的目标非常接近。
我希望有一个 ScrollView,它可以在我的 MythTV 机顶盒上显示即将录制的内容。有一个显示日期的 header 小部件(固定大小)和一个用于记录详细信息的单独小部件 (BoxLayout)(显示在标签 child 小部件中)。
我需要根据文本的高度调整 BoxLayout 的大小。这里的问题是,文本最多的标签并不总是相同的,因此最大高度需要考虑所有 children.
我的 KV 语言文件中有这个:
<MythRecordingHeader>
text: root.rec_date
height: 25
size_hint_y: None
halign: "left"
<MythRecording>
orientation: "horizontal"
#height: 60
height: max(mrec_desc.texture_size[1], mrec_time.texture_size[1], mrec_title.texture_size[1])
size_hint_y: None
BGLabel:
id: mrec_time
size_hint: 0.2, None
text_size: self.width, None
height: max(self.texture_size[1], mrec_title.texture_size[1], mrec_desc.texture_size[1])
#size_hint_x: 0.2
text: root.rec["time"]
bgcolour: root.bg
BGLabel:
id: mrec_title
size_hint: 0.3, None
text_size: self.width, None
height: max(self.texture_size[1], mrec_time.texture_size[1], mrec_desc.texture_size[1])
#size_hint_x: 0.3
text: "{}\n{}".format(root.rec["title"], root.rec["subtitle"])
halign: "left"
bgcolour: root.bg
BGLabel:
id: mrec_desc
text: root.rec["desc"]
size_hint: 0.5, None
text_size: self.width, None
height: max(self.texture_size[1], mrec_time.texture_size[1], mrec_title.texture_size[1])
halign: "left"
bgcolour: root.bg
(BGLabel
是一个自定义小部件,它只是通过 bgcolour
属性 向标签添加纯色背景色)。
在我的 python 文件中:
class MythRecording(BoxLayout):
rec = DictProperty({})
bg = ListProperty([0.1, 0.15, 0.15, 1])
def __init__(self, **kwargs):
super(MythRecording, self).__init__(**kwargs)
self.rec = kwargs["rec"]
class MythRecordingHeader(BGLabel):
rec_date = StringProperty("")
def __init__(self, **kwargs):
super(MythRecordingHeader, self).__init__(**kwargs)
self.bgcolour = [0.1, 0.1, 0.4, 1]
self.rec_date = kwargs["rec_date"]
这几乎符合我的预期 - BoxLayouts 的高度确实有所不同,并且总体上显示了所有文本,但有一两次有一些剪裁。我不清楚这是否是由于水平对齐造成的。
我目前将所有这些 children 放在一个 BoxLayout 中。
但是,我真正的问题是我需要将它放在 ScrollView 中(因此我提到了 link)。为了让滚动正常工作,我想我需要设置 BoxLayout 的高度,但我真的很难计算它的高度。
遍历 BoxLayout 的 children 并查看 height
属性 只显示 MythRecordings
的高度为 0。
有没有办法获取所有 children 的总高度并设置 BoxLayout 的高度以便我的滚动视图正常工作?
用 GridLayout 替换 BoxLayout,并设置:
size: self.minimum_size
GridLayout 的 minimum_size 属性 自动跟踪其子项的大小。