0

我有一个界面布局(cleanscreen.kv):

#:kivy 1.9.1

<CleanScreen>
    FloatLayout:
        canvas:
            Color:
                rgb:
                    0.1, 0.3, 0.6
            Rectangle:
                pos: self.pos
                size: self.size

    ScrollView:
        GridLayout:
            cols: 1
            size_hint_y: None
            spacing: 10
            padding: 10
            height: self.minimum_height
            canvas:
                Color:
                    rgb: 1, 0, 1
                Rectangle:
                    pos: self.pos
                    size: self.size
            FloatLayout:
                id: box_share
                size_hint_y: None

和 Python 文件(cleanscreen.py):

#! /usr/bin/python2.7
# -*- coding: utf-8 -*-

try:
    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.image import Image
    from kivy.uix.button import Button
    from kivy.uix.label import Label
    from kivy.lang import Builder
except Exception as text_error:
    raise text_error


class CleanScreen(BoxLayout):
    Builder.load_file("cleanscreen.kv")

    def __init__(self, **kvargs):
        super(CleanScreen, self).__init__(**kvargs)
        self.orientation = "vertical"
        self.create_button(self.ids.box_share)

    def create_button(self, box_share):
        top_logo_share = 1.01
        top_button_share = 1.1
        top_label_share = 1.4

        for i in range(50):
            top_logo_share -= .4
            top_button_share -= .4
            top_label_share -= .4

            logo_share = \
                Image(source="data/logo/kivy-icon-48.png",
                      pos_hint={"center_x": .05, "top": top_logo_share},
                      size_hint_y=None, height=25)
            button_share = \
                Button(pos_hint={"x": 0, "top": top_button_share},
                       size_hint_y=None, height=40)
            label_share = \
                Label(text=str(i), pos_hint={"x": 0, "top": top_label_share},
                      size_hint_y=None)

            box_share.add_widget(button_share)
            box_share.add_widget(logo_share)
            box_share.add_widget(label_share)

if __name__ in ["__main__", "__android__"]:
    import kivy
    kivy.require("1.9.1")


    class Test(App):
        def build(self):
            return CleanScreen()


    Test().run()

我想得到这个结果:

我想得到这个结果

运行此脚本会在此处显示以下结果:

但到达这里是

并且列表没有滚动按钮​​。我在哪里犯了错误?

4

1 回答 1

0

FloatLayout的 in kv 文件没有设置高度,因此获取默认值。GridLayout这实际上并不重要,因为您只需将小部件推送到一个小部件中,当它在里面时它不会有任何好处ScrollView。不,有一种更好、更合理的方法(至少我是这么看的)。:)

扔掉FloatLayoutkv 文件,不需要它并使用GridLayout,因为ScrollView当您将小部件推入时,这是应该变大(并影响)的小部件,而不是FloatLayout强制 GridLayout 人为地使自己变大。

ScrollView:
    GridLayout:
        id: box_share
        cols: 1
        size_hint_y: None
        spacing: 10
        padding: 10
        height: self.minimum_height
        canvas:
            Color:
                rgb: 1, 0, 1
            Rectangle:
                pos: self.pos
                size: self.size

并在 python 文件中使用create_button()创建一个FloatLayout你用你的小部件填充的。我对那个定位有点困惑,所以我不会为你解决这个问题,抱歉。不要忘记设置的高度FloatLayout

def create_button(self, box_share):
    top_logo_share = 1.01
    top_button_share = 1.1
    top_label_share = 1.4

    for i in range(50):
        top_logo_share -= .4
        top_button_share -= .4
        top_label_share -= .4

        logo_share = \
            Image(source="data/logo/kivy-icon-48.png",
                  pos_hint={"center_x": .05, "top": top_logo_share},
                  size_hint_y=None, height=25)
        button_share = \
            Button(pos_hint={"x": 0, "top": top_button_share},
                   size_hint_y=None, height=40)
        label_share = \
            Label(text=str(i), pos_hint={"x": 0, "top": top_label_share},
                  size_hint_y=None)
        fl = FloatLayout(size_hint_y=None, height=25)
        fl.add_widget(button_share)
        fl.add_widget(logo_share)
        fl.add_widget(label_share)
        box_share.add_widget(fl)
于 2016-04-28T19:35:51.777 回答