我有大量文本要以滚动视图的形式显示在屏幕上,因此我要字符串化以生成滚动视图,并添加一个gridlayout(或任何布局)以填充每个带有文本的标签。我的代码如下: KV:
ScrollView:
#The scrollview that the gridlayout filled with labels is added to.
size_hint: 1, .8
id: content_holder_kv
PY:
layout = GridLayout(cols=1, spacing=10, size_hint_y=None)
#The gridlayout that the labels will be added to and that will then be added to the scrollview.
layout.bind(minimum_height=layout.setter('height'))
for i in range(1141):
temp = ("Long stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong string")
line_of_text = Label(text=temp,text_size=(self.size))
#This is the part ^ that I think is causing the issues. When I didnt have(text_size=(self.size)) the labels just went off the end of the screen. I have attempted adding text size as well as other attributes to try to get the wrapping behavior I need where the text label uses the entire width of the screen, is multiline, and doesnt overlap text with other labels in the scrollview but I have been unable to achieve this behavior.
layout.add_widget(line_of_text)
self.content_holder_py.add_widget(layout)
一些我认为不重要的事情,但也许我想提一提,我的应用程序具有多个屏幕,并且由于要显示一本书的章节,所以我试图将行分开。文本行是本书中的句子/段落,我需要在不与其他文本标签重叠的情况下进行包装。下面是此代码产生的图片(尝试了其他布局设置选项,并且不确定导致该问题的原因)。
答案 0 :(得分:1)
可以垂直增长但将文本以一定宽度包裹的标签:
<CustomLabel>:
size_hint_y: None
text_size: self.width, None
height: self.texture_size[1]
要在X轴/水平方向和Y轴/垂直方向上滚动GridLayout,请将size_hint属性设置为(无,无)。
ScrollView - ScrollEffect, scroll_type,bar_width等
默认情况下,ScrollView允许同时在X和Y轴上滚动。通过将do_scroll_x或do_scroll_y属性设置为False,可以显式禁用在轴上滚动。
要在Y轴上/垂直滚动GridLayout,请设置子项的 宽度为ScrollView的宽度(size_hint_x = 1),然后将 将size_hint_y属性设置为“无”:
当滚动超出ScrollView的范围时,它将使用ScrollEffect处理超滚动。
设置用于滚动视图内容的滚动类型。 可用的选项包括:['内容'],['栏'],['栏','内容']
[“ bars”]可以通过拖动或滑动scoll栏来滚动内容。
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.properties import ObjectProperty
class CustomLabel(Label):
pass
class RootWidget(BoxLayout):
content_holder_py = ObjectProperty(None)
def __init__(self, **kwargs):
super(RootWidget, self).__init__(**kwargs)
temp = "Long stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong stringLong string"
for i in range(1141):
line_of_text = CustomLabel(text=temp)
self.content_holder_py.add_widget(line_of_text)
class TestApp(App):
title = "Kivy ScrollView of Labels Demo"
def build(self):
return RootWidget()
if __name__ == "__main__":
TestApp().run()
#:kivy 1.11.0
#:import Window kivy.core.window.Window
<CustomLabel>:
size_hint_y: None
text_size: self.width, None
height: self.texture_size[1]
<RootWidget>:
orientation: "vertical"
content_holder_py: content_holder_kv
ScrollView:
bar_width: 10
bar_color: 0, 0, 1, 1 # blue
bar_inactive_color: 1, 0, 0, 1 # red
effect_cls: "ScrollEffect"
scroll_type: ['bars']
size_hint: (1, None)
size: (Window.width, Window.height)
GridLayout:
id: content_holder_kv
cols: 1
spacing: 10
size_hint_y: None
height: self.minimum_height