【问题标题】:Kivy. Position of GridLayout inside ScrollView基维。 GridLayout 在 ScrollView 内的位置
【发布时间】:2019-08-16 15:46:54
【问题描述】:

我放弃了。我想我已经尽力了。我需要一些解释如何定位嵌套布局类。在下面的代码中,我需要将这些标签定位在屏幕的中心。我试过的任何东西都会在左侧留下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()

【问题讨论】:

    标签: kivy


    【解决方案1】:

    使Labels 居中的最简单方法是让GridLayout 调整大小并定位它们。这会导致每个 Labelwidth 更大,但它们是居中的:

    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)中,然后让GridLayout 调整大小并定位那些Layouts

    from kivy.app import App
    from kivy.lang import Builder
    
    kv = """
    
    <StorageLabel@Label>
        background_normal: ''
        size_hint: None, None
        size: 65, 50
        # position the label in the center of its FloatLayout
        pos_hint: {'center_x':0.5}
        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
                size_hint_y: None
                height: self.minimum_height
                # set the default row height to the height of the Labels
                row_default_height: 50
    
                FloatLayout:
                    StorageLabel:
                        text: '1m'
    
                FloatLayout:
                    StorageLabel:
                        text: '2m'
    
                FloatLayout:
                    StorageLabel:
                        text: '3m'
    
    Storage:
    
    """
    
    sm = Builder.load_string(kv)
    
    class Main(App):
        def build(self):
            return sm
    
    if __name__ == '__main__':
        Main().run()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-15
      相关资源
      最近更新 更多