基维GridLayout在ScrollView中的位置

时间:2019-08-16 15:46:54

标签: kivy

我放弃。我想我已经尽力了。我需要一些如何放置嵌套布局类的知识。在下面的代码中,我需要将这些标签放置在屏幕中央。我尝试过的所有操作都将Labels留在左侧。

from kivy.app import App
from kivy.lang import Builder

kv = """

<StorageLabel@Label>
    background_normal: ''
    size_hint: None, None
    size: 65, 50
    canvas.before:
        Color:
            rgba: (1, 0, 0.5, 1) 
        Rectangle:
            pos: self.pos
            size: self.size
    halign: "left"
    valign: "middle"    

<Storage@BoxLayout>
    ScrollView:
        size_hint_x: 1
        bar_width: 10
        scroll_type: ['bars', 'content']
        bar_color: [0, 0, 0, 1]
        bar_inactive_color: [0, 0, 0, 1]
        pos_hint: {'center_x': 0.5}
        GridLayout:

            cols: 3
            size_hint_y: None
            size: self.minimum_size
            height: self.minimum_height

            StorageLabel:
                text: '1m'

            StorageLabel:
                text: '2m'

            StorageLabel:
                text: '3m'

Storage:

"""

sm = Builder.load_string(kv)

class Main(App):
    def build(self):
        return sm

if __name__ == '__main__':
    Main().run()

1 个答案:

答案 0 :(得分:1)

使Labels居中的最简单方法是设置GridLayout的大小并放置它们。这样会导致每个width的{​​{1}}更大,但是它们居中:

Label

如果您希望标签为原始尺寸,则可以将每个标签放置在自己的from kivy.app import App from kivy.lang import Builder kv = """ <StorageLabel@Label> background_normal: '' # leave size_hint_x at default of 1 size_hint_y: None height: 50 canvas.before: Color: rgba: (1, 0, 0.5, 1) Rectangle: pos: self.pos size: self.size <Storage@BoxLayout> ScrollView: size_hint_x: 1 bar_width: 10 scroll_type: ['bars', 'content'] bar_color: [0, 0, 0, 1] bar_inactive_color: [0, 0, 0, 1] pos_hint: {'center_x': 0.5} GridLayout: cols: 3 # add some padding and spacing padding: 5 spacing: 5 size_hint_y: None height: self.minimum_height StorageLabel: text: '1m' StorageLabel: text: '2m' StorageLabel: text: '3m' Storage: """ sm = Builder.load_string(kv) class Main(App): def build(self): return sm if __name__ == '__main__': Main().run() (也许是Layout)中,并让FloatLayout大小和位置{ {1}}:

GridLayout