首页 > 解决方案 > 如何在浮动布局中固定小部件的位置 - kivy

问题描述

我有一个与 pos_hint 和 floatlayout kivy 中的小部件定位有关的大问题。所以现在基本上我有两个 y_scroll 设置为 True 的 MDTextField。这样它们的大小就会在每个包裹上增加。因此这些小部件一个接一个地放置,使得 input_field_1 首先是 input_field_2。这些小部件当然放置在 FlaotLayout 中,而这个 FlaotLayout 位于 ScrollView 中。每当文本字段的高度增加时,它也会导致 floatlayout 的高度增加。所以我的问题是,如果你先写 20 行字段,使其高度比此时增加 20 倍,它将与第二个小部件重叠。并且它们将没有该小部件的迹象。我怎样才能避免这样的事故和任何帮助表示赞赏。

我的代码如下:


str = '''

ScrollView:
    do_scroll_x : False
    FloatLayout:
        height : input_field_1.height + input_field_2.height + 100
        size_hint : None,None
        
        MDTextField:
            id : input_field_1
            mode : 'rectangle'
            size_hint : None,None
            pos_hint : {'x' : 0.1,'top': 0.8}
            y_scroll : True
            multiline : True
        MDTextField:
            id : input_field_2
            mode : 'rectangle'
            size_hint : None,None
            pos_hint : {'x' : 0.1,'top': 0.5}
            y_scroll : True
            multiline : True

'''
class My_4app(MDApp):
    def build(self):
        return Builder.load_string(str)
My_4app().run()

如果您认为他们是否有更好的小部件,请建议

标签: kivyposition

解决方案


我认为 aBoxLayout会做你想做的事。尝试更换:

FloatLayout:
    height : input_field_1.height + input_field_2.height + 100
    size_hint : None,None

和:

BoxLayout:
    orientation: 'vertical'
    size_hint: 1, None
    height: self.minimum_height

推荐阅读