【问题标题】:My RelativeLayout apparently ignores my pos_hint我的 RelativeLayout 显然忽略了我的 pos_hint
【发布时间】:2020-12-01 14:15:54
【问题描述】:

我一直在尝试为我的应用程序制作 GUI,并且我已经了解了一些关于 RelativeLayout 的知识。
根据我的理解,您定义了一个变量“pos_hint”,并分配一个从 0 到 1 的值,表示它与该位置的接近程度。例如。 “右”:1 == 100% 向右

问题是,我有一个垂直的 BoxLayout,包含 2 个 RelativeLayout,其中包含小部件,其中一个也有自己的 RelativeLayout。
这变得非常复杂,我正在努力寻找答案的问题。

我的目标是:
“SuspectGraph”在顶部中间,其中包含 2 个相互重叠的椭圆。”
“SettingButton”在右上角。
“MoodButton”位于 y 中间,1/4 位于底部框的 x 处
“FoodButton”位于 y 中间,3/4 位于底部框底框

从完整的答案到关于如何修复错误的轻微提示,我们非常感谢任何帮助。

编辑:忘记添加代码。对不起。

<MainLayout>:
    canvas:
        Color:
            rgba: 1, 1, 1, 1
        Rectangle:
            pos: 0,0
            size: self.width,self.height
    BoxLayout:
        size: self.parent.size
        orientation: "vertical"
        RelativeLayout:
            canvas:
                Color:
                    rgba: 1,0,0,1
                Rectangle:
                    size: self.width,self.height
            size: self.parent.size
            SettingButton:
                size: 50,100
                pos_hint: {'right':1,'y':1}
                Button:
                    text: "Settings"
                    size: self.size
            SuspectGraph:
                pos_hint: {'center_x':1,'center_y':1}
                RelativeLayout:
                    GraphInnerCircle:
                        pos_hint: {'center_x':1,'center_y':1}
                        size: 200,200
                        canvas:
                            Color:
                                rgba: .5,.5,.5,1
                            Ellipse:
                                size: self.size
                    GraphOuterCircle:
                        pos_hint: {'center_x':1,'center_y':1}
                        size: 300,300
                        canvas:
                            Color:
                                rgba: .3,.3,.3,1
                            Ellipse:
                                size: self.size
        RelativeLayout:
            MoodButton:
                size: 50,100
                pos_hint:{'left':.1,'center_y':.7}
                Button:
                    text: "Register Mood"
            FoodButton:
                size: 50,100
                pos_hint:{'right':.1,'center_y':.7}
                Button:
                    text: "Register Food"

【问题讨论】:

    标签: python kivy android-relativelayout


    【解决方案1】:

    问题在于属性如何在嵌套小部件上工作。
    以这个 sn-p 为例:

            size: self.parent.size
            orientation: "vertical"
            RelativeLayout: 
                canvas:
                    Color:
                        rgba: 1,0,0,1
                    Rectangle:
                        size: self.width,self.height
                size: self.parent.size
                SettingButton:
                    size: 50,100
                    pos_hint: {'right':1,'y':1}
                    Button:
                        text: "Settings"
                        size: self.size
    

    第 12 行的 pos_hint 适用于小部件“SettingButton”,但不适用于 里面的按钮。 我不知道为什么,但这就是它的工作原理。

    尝试将 pos_hint 直接设置为 Button 不起作用,因为它将尝试从 SettingButton 推断其位置,而不是布局。

    解决方案
    将 SettingButton 的 python 端代码更改为 Button 而不是 Widget。
    将 pos_hint 直接应用到 SettingButton。

    希望这可以帮助任何在谷歌上搜索相同问题的人!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 2011-12-22
      • 2017-12-09
      • 2018-09-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多