gpt4 book ai didi

android - 在 kivy 的 ScrollView 中将文本与 Label 的边缘对齐

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:53:10 25 4
gpt4 key购买 nike

我正在尝试使 Label 水平滚动,并希望 halign:"rightvalign:middle 按照下面的代码

ScrollView:

Label:
id:maindisplay
text:"0"
font_size:"50sp"
text_size: None ,self.height[1] # Set the text wrap box height
size_hint_x:None
width: self.texture_size[0]

#Following part is not working
halign: 'right'
valign: 'middle'

enter image description here

最佳答案

这里的关键点是文本的宽度。如果设置为None,则不受约束,如果文本比父级宽,则可以滚动。但是,如果文本只需要父级宽度的一小部分,则文本将在标签内居中(尽管多行仍将右对齐)。另一方面,如果 text_size 静态设置为有限值,不适合的文本将换行并且永远不会滚动。 hacky 解决方案是设置 text_size: 8000, self.height(非常宽,不太可能发生)。这允许滚动,但是很丑陋(并且可能会不小心滚动到没有显示任何内容的区域)。

相反,我们动态更改 text_width。当 text 改变时,我们首先移除宽度限制。然后,我们让标签更新其内容,并将 text_size 设置为适合文本所需的宽度,但至少与父级一样宽。

其他设置确保标签本身至少与父级一样宽 (width) 并且 ScrollView 从右边缘开始 (scroll_x :1)

这是一个完整的例子:

from kivy.app import App
from kivy.lang import Builder
from kivy.config import Config
from kivy.clock import Clock
from kivy.properties import StringProperty, NumericProperty

kv = '''ScrollView:
scroll_x: 1
Label:
canvas.before:
Color:
rgba:1,0,0,1
Rectangle:
pos: self.parent.pos
size: self.size
id:maindisplay
text:"2"
font_size:"50sp"
text_size: None, self.height
size_hint_x: None
width: max(self.texture_size[0], self.parent.width)
halign: 'right'
valign: 'middle'
on_text: app.txt_cb(*args)
'''

class QApp(App):
txt = StringProperty("1")
num = NumericProperty(0)

def add_string(self, dt=0):
self.num += 1
self.txt += "%i" % self.num
self.root.ids.maindisplay.text = self.txt

def txt_cb(self, obj, text):
obj.text_size = (None, obj.height)
obj.texture_update()
obj.text_size = (max(obj._label.content_width, obj.parent.width), obj.height)

def build(self):
Clock.schedule_once(lambda dt:self.txt_cb(self.root.ids.maindisplay, ""), 0)
Clock.schedule_interval(self.add_string, .5)
return Builder.load_string(kv)

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

关于android - 在 kivy 的 ScrollView 中将文本与 Label 的边缘对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35099375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com