如何将 BoxLayout 定位到屏幕顶部
How to position BoxLayout to top of screen
我有一个嵌套在 BoxLayout 小部件中的 ActionBar。我希望 ActionBar 出现在屏幕的最顶部。目前,我只能让它出现在最底部。这是屏幕截图:
这是我的 main.py:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class ShowActionBar(BoxLayout):
pass
class MyTestApp(App):
pass
if __name__ == "__main__":
MyTestApp().run()
这是我的 .kv 文件:
ShowActionBar:
<ShowActionBar>:
orientation: "vertical"
BoxLayout:
pos_hint: {'top': 1}
orientation: "vertical"
ActionBar:
ActionView:
use_seperator: True
ActionPrevious:
title: "My Title"
with_previous: False
ActionOverflow:
ActionButton:
text: "Settings"
ActionButton:
text: "Log Out"
如您所见,我已尝试使用 pos_hint 的各种值,但它似乎不会影响 ActionBar 的位置。我怀疑我遗漏了一些简单的东西。我需要做什么才能让 ActionBar 出现在 window 的最顶部?
您的示例实际上有点太简单了 - 只要添加另一个小部件,您就会得到正确的行为。例如,这是您的小部件的新规则:
<ShowActionBar>:
orientation: "vertical"
ActionBar:
ActionView:
use_seperator: True
ActionPrevious:
title: "My Title"
with_previous: False
ActionOverflow:
ActionButton:
text: "Settings"
ActionButton:
text: "Log Out"
Label:
text: 'the next widget added fills all the extra space'
text_size: self.size
halign: 'center'
valign: 'middle
请注意,我还删除了不必要的 BoxLayout 级别。
真正的问题是 BoxLayout 在只有一个 child 时会忽略 pos_hint。也许它算作一个错误,但充其量只是一个小错误,因为它并不是真正打算处理固定大小的单个 child - FloatLayout 更适合这个。但是,一旦您添加更多 children.
,BoxLayout 可能正是您想要的
我有一个嵌套在 BoxLayout 小部件中的 ActionBar。我希望 ActionBar 出现在屏幕的最顶部。目前,我只能让它出现在最底部。这是屏幕截图:
这是我的 main.py:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class ShowActionBar(BoxLayout):
pass
class MyTestApp(App):
pass
if __name__ == "__main__":
MyTestApp().run()
这是我的 .kv 文件:
ShowActionBar:
<ShowActionBar>:
orientation: "vertical"
BoxLayout:
pos_hint: {'top': 1}
orientation: "vertical"
ActionBar:
ActionView:
use_seperator: True
ActionPrevious:
title: "My Title"
with_previous: False
ActionOverflow:
ActionButton:
text: "Settings"
ActionButton:
text: "Log Out"
如您所见,我已尝试使用 pos_hint 的各种值,但它似乎不会影响 ActionBar 的位置。我怀疑我遗漏了一些简单的东西。我需要做什么才能让 ActionBar 出现在 window 的最顶部?
您的示例实际上有点太简单了 - 只要添加另一个小部件,您就会得到正确的行为。例如,这是您的小部件的新规则:
<ShowActionBar>:
orientation: "vertical"
ActionBar:
ActionView:
use_seperator: True
ActionPrevious:
title: "My Title"
with_previous: False
ActionOverflow:
ActionButton:
text: "Settings"
ActionButton:
text: "Log Out"
Label:
text: 'the next widget added fills all the extra space'
text_size: self.size
halign: 'center'
valign: 'middle
请注意,我还删除了不必要的 BoxLayout 级别。
真正的问题是 BoxLayout 在只有一个 child 时会忽略 pos_hint。也许它算作一个错误,但充其量只是一个小错误,因为它并不是真正打算处理固定大小的单个 child - FloatLayout 更适合这个。但是,一旦您添加更多 children.
,BoxLayout 可能正是您想要的