我在示例应用程序中使用 Accordion 小部件(用于学习 Kivy)。 在我的 KV 代码中,我有:
Accordion:
orientation: 'vertical'
AccordionItem:
title: '2024/12/20 00:00 <reference>'
...
标题显示在 AccordionItem 标题的中央。 我想更改此设置以使所有标题向左对齐。 但我在 Accordion 小部件的文档中没有看到执行此操作的方法。
有没有简单的方法来做这个简单的事情???
根据文档,您可以调整
title_template
,它描述了title
的显示方式。尝试将其添加到您的 kv
文件中:
[AccordionItemTitle@Label]:
text: ctx.title
halign: 'left'
valign: 'center'
text_size: self.size
normal_background: ctx.item.background_normal if ctx.item.collapse else ctx.item.background_selected
disabled_background: ctx.item.background_disabled_normal if ctx.item.collapse else ctx.item.background_disabled_selected
canvas.before:
Color:
rgba: self.disabled_color if self.disabled else self.color
BorderImage:
source: self.disabled_background if self.disabled else self.normal_background
pos: self.pos
size: self.size
PushMatrix
Translate:
xy: self.center_x, self.center_y
Rotate:
angle: 90 if ctx.item.orientation == 'horizontal' else 0
axis: 0, 0, 1
Translate:
xy: -self.center_x, -self.center_y
canvas.after:
PopMatrix
这只是添加:
halign: 'left'
valign: 'center'
text_size: self.size
到模板。它可以调整
title
来执行您想要的操作。请参阅标签了解发生了什么。