【问题标题】:Kivy RelativeLayout vs FloatLayoutKivy RelativeLayout 与 FloatLayout
【发布时间】:2018-08-16 16:26:49
【问题描述】:

这是来自 Kivy 相对布局的文档。 https://kivy.org/docs/api-kivy.uix.relativelayout.html

相对布局:此布局允许您设置相对坐标 为儿童。如果要绝对定位,请使用 FloatLayout。 RelativeLayout 类的行为就像常规的 FloatLayout 除了它的子小部件相对于布局定位。 当 position = (0,0) 的小部件被添加到 RelativeLayout 时, 当 RelativeLayout 的位置为 改变了。子小部件坐标始终保持 (0,0) 相对于父布局。

我看到 Floatlayout 也有同样的作用。实际上,Floatlayout 和RelativeLayout 都支持绝对定位和相对定位,具体取决于使用的是pos_hint 还是pos。

在现实中也不管绝对和相对定位,当布局位置改变时,小部件也会移动。

【问题讨论】:

    标签: android python android-layout kivy


    【解决方案1】:

    FloatLayout:此布局以比例坐标组织小部件 size_hint 和 pos_hint 属性。值是数字 介于 0 和 1 之间,表示与窗口大小的比例。

    相对布局:此布局的操作方式与 FloatLayout 相同,但 定位属性(pos、x、center_x、right、y、center_y 和 top) 是相对于布局大小而不是窗口大小的。

    可用的 pos_hint 键(x、center_x、right、y、center_y 和 top)很有用 用于对齐边缘或居中。例如 pos_hint: {'center_x':.5, 'center_y':.5} 会在中间对齐一个 Widget,不管它的大小是多少 窗户是。

    【讨论】:

    • 请参考我作为答案发布的回复
    【解决方案2】:

    参考下面的python程序。 FloatVerification 和 RelativeVerification 类在窗口大小和布局大小方面的行为方式相同。有什么我想念的方面吗?

    from kivy.app import App 
    
    from kivy.uix.floatlayout import FloatLayout
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.relativelayout import RelativeLayout
    
    from kivy.uix.button import Button
    
    class FloatVerification(App):
        def build(self):
            bl  = BoxLayout()
    
            fl1 = FloatLayout()
            fl2 = FloatLayout()
    
    
            b1 = Button(size_hint=(.5,.5),
                        pos_hint={'top':.5,'right':.5},
                        text="Hello")
            b2 = Button(size_hint=(.5,.5),
                        pos_hint={'top':.5,'right':.5},
                        text="Hello")
    
            fl1.add_widget(b1)
            fl2.add_widget(b2)
    
            bl.add_widget(fl1)
            bl.add_widget(fl2)
    
            return bl
    
    class RelativeVerification(App):
        def build(self):
            bl  = BoxLayout()
    
            rl1 = RelativeLayout()
            rl2 = RelativeLayout()
    
    
            b1 = Button(size_hint=(.5,.5),
                        pos_hint={'top':.5,'right':.5},
                        text="Hello")
            b2 = Button(size_hint=(.5,.5),
                        pos_hint={'top':.5,'right':.5},
                        text="Hello")
    
            rl1.add_widget(b1)
            rl2.add_widget(b2)
    
            bl.add_widget(rl1)
            bl.add_widget(rl2)
    
            return bl
    
    if __name__  == "__main__":
        FloatVerification().run()
        #RelativeVerification().run()
    

    【讨论】:

    • 这是对 Julio CampPlaz 的回答
    • RelativeLayout 是相对的,因为“pos”是相对的,“pos_hint”在 FloatLayout 和 RelativeLayout 中都是相对的,而“pos”在 RelativeLayout 中只是相对的。
    猜你喜欢
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-26
    • 2017-06-17
    • 1970-01-01
    • 2020-01-22
    相关资源
    最近更新 更多