如何在 Kivy 中创建一个自动滚动标签?
How can I create an automatic scrollable label in Kivy?
我正在开发一个应用程序,顶部有一些按钮,底部有一个可滚动的 Label
。我想让可滚动标签自动从右向左滚动,就像人们在新闻上看到的那样。
<marquee>N e w s</marquee>
我知道如何制作可滚动标签,但我不确定如何自动移动它。
只需将顶级布局设为 ScrollView,如下所示:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
</ScrollView>
如果您想详细滚动查看,请连接到此站点:
https://developer.android.com/reference/android/widget/ScrollView.html
要制作一个可滚动的标签,有一小段代码可以直接从 kv
lang 中使用。先把size_hint_x
设置成None
,这样就可以放大到texture_size
的宽度了。
from kivy.lang import Builder
from kivy.base import runTouchApp
from kivy.animation import Animation
from kivy.uix.scrollview import ScrollView
Builder.load_string('''
<Spacer@Widget>:
size_hint_x: None
width: 800
<ScrollLabel>:
GridLayout:
rows: 1
size_hint_x: None
width: self.minimum_width
Spacer:
Label:
size_hint_x: None
text: 'l or emi psum '*100
width: self.texture_size[0]
Spacer:
''')
class ScrollLabel(ScrollView): pass
scroll = ScrollLabel(scroll_y=-1)
marquee = Animation(scroll_x=1, duration=100.0)
marquee.start(scroll)
runTouchApp(scroll)
要使其滚动,只需使用 Animation
。您还可以将 Widget
用作 spacer,这样您的文本看起来就像 html marquee
标签,即滚动到一个空的 space,而不是滚动已经可见的文本。
我正在开发一个应用程序,顶部有一些按钮,底部有一个可滚动的 Label
。我想让可滚动标签自动从右向左滚动,就像人们在新闻上看到的那样。
<marquee>N e w s</marquee>
我知道如何制作可滚动标签,但我不确定如何自动移动它。
只需将顶级布局设为 ScrollView,如下所示:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
</ScrollView>
如果您想详细滚动查看,请连接到此站点:
https://developer.android.com/reference/android/widget/ScrollView.html
要制作一个可滚动的标签,有一小段代码可以直接从 kv
lang 中使用。先把size_hint_x
设置成None
,这样就可以放大到texture_size
的宽度了。
from kivy.lang import Builder
from kivy.base import runTouchApp
from kivy.animation import Animation
from kivy.uix.scrollview import ScrollView
Builder.load_string('''
<Spacer@Widget>:
size_hint_x: None
width: 800
<ScrollLabel>:
GridLayout:
rows: 1
size_hint_x: None
width: self.minimum_width
Spacer:
Label:
size_hint_x: None
text: 'l or emi psum '*100
width: self.texture_size[0]
Spacer:
''')
class ScrollLabel(ScrollView): pass
scroll = ScrollLabel(scroll_y=-1)
marquee = Animation(scroll_x=1, duration=100.0)
marquee.start(scroll)
runTouchApp(scroll)
要使其滚动,只需使用 Animation
。您还可以将 Widget
用作 spacer,这样您的文本看起来就像 html marquee
标签,即滚动到一个空的 space,而不是滚动已经可见的文本。