【发布时间】:2020-09-17 17:08:41
【问题描述】:
我正在尝试在 Kivy/KivyMD 中创建响应式、居中和可滚动的布局。到目前为止,我已经开发了两种布局,一种将 BoxLayout 置于 FloatLayout 的中心,并且可以很好地调整大小,但它不会滚动。 我还开发了一个布局,它是 ScrollView 中的 GridLayout,它可以很好地调整大小,但是在大窗口大小时,它不能正确地将 GridLayout 居中。
我尝试将 BoxLayout 放在 ScrollView 中的 FloatLayout 中,它可以很好地调整大小但滚动条不起作用。
到目前为止,我无法将居中的可调整大小的布局组合到正常工作的滚动视图中。我应该如何做到这一点?
这是我的滚动视图示例:
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.scrollview import ScrollView
from kivy.uix.floatlayout import FloatLayout
LIPSUM = """Very long lipsum..."""
Builder.load_string("""
<ExampleScroll@ScrollView>:
do_scroll_x: False
bar_width: 10
bar_color: app.theme_cls.primary_color
bar_color_acrive: app.theme_cls.accent_color
effect_cls: "DampedScrollEffect"
scroll_type: ['bars']
GridLayout: # If FloatLayout and BoxLayout, doesn't scroll!
cols: 1
size_hint_y: None
height: self.minimum_height
size_hint_x: .75
size_hint_max_x: dp(800)
size_hint_min_x: min(dp(400), root.width)
pos_hint: {'center_x': .5} # Fails to center layout
padding: 0, dp(16), 0, 0
MDLabel:
text: app.label_text + app.label_text
halign: 'justify'
padding: dp(16), dp(16)
markup: True
font_style: 'Body1'
theme_text_color: 'Primary'
size_hint_y: None
height: self.texture_size[1]
text_size: self.size
MDLabel:
text: app.label_text + app.label_text
halign: 'justify'
padding: dp(16), dp(16)
markup: True
font_style: 'Body1'
theme_text_color: 'Primary'
size_hint_y: None
height: self.texture_size[1]
text_size: self.size
Widget
""")
class ExampleScroll(ScrollView):
pass
class Example(MDApp):
title = "Dialogs"
label_text = LIPSUM
def build(self):
return ExampleScroll()
Example().run()
这是我的居中示例:
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.scrollview import ScrollView
from kivy.uix.floatlayout import FloatLayout
LIPSUM = """Very long lipsum..."""
Builder.load_string("""
<ExampleCenter@FloatLayout>:
BoxLayout:
orientation: 'vertical'
# Gives the BoxLayout a max and min width that is responsive
size_hint_x: .75
size_hint_max_x: dp(800)
size_hint_min_x: min(dp(400), root.width)
# Centers the BoxLayout horizontally responsively
pos_hint: {'center_x': .5}
padding: 0, dp(16), 0, 0
MDLabel:
text: app.label_text + app.label_text
halign: 'justify'
padding: dp(16), dp(16)
markup: True
font_style: 'Body1'
theme_text_color: 'Primary'
size_hint_y: None
height: self.texture_size[1]
MDLabel:
text: app.label_text + app.label_text
halign: 'justify'
padding: dp(16), dp(16)
markup: True
font_style: 'Body1'
theme_text_color: 'Primary'
size_hint_y: None
height: self.texture_size[1]
Widget
""")
class ExampleCenter(FloatLayout):
pass
class Example(MDApp):
title = "Dialogs"
label_text = LIPSUM
def build(self):
return ExampleCenter()
Example().run()
我怎样才能编译它们并使其工作?
【问题讨论】:
标签: python-3.x kivy kivy-language