Kivy网格布局修复了某些列

时间:2015-12-20 15:30:53

标签: python image kivy grid-layout

我想构建一个网格布局,其中一些列具有固定宽度(包含图像),而其他列应占用可用空间。这是我目前的情况(最后一栏):

kivy grid layout example

正如您在蓝色背景上看到的那样,列对于图像来说太大了。但是,由于有更多列的大小应该相对于可用宽度,我不能使用网格布局的col_force_default属性。

那么是否可以修复某些列的宽度,同时让其他列占用所有可用空间?

以下是我的kv文件的摘录:

<EditWorkoutExcerciseRow@GridLayout>
    exRepetitionsRound: ex_repetitions_round
    exRepetitionsText: ex_repetitions_ti
    exNameLabel: ex_name_label
    exDeleteBtn:ex_delete_btn
    exUpBtn:ex_up_btn
    exDownBtn: ex_down_btn


    TextInput:
        id: ex_repetitions_round
        multiline:False
        size_hint:(0.25,1)
        padding:(10,10,10,10)

    TextInput:
        size_hint:(0.25,1)
        id: ex_repetitions_ti
        multiline:False
        padding:(10,10,10,10)

    Label:
        size_hint:(0.5,1)
        id: ex_name_label
        text:''


    Button:
        id:ex_delete_btn
        background_normal:''
        background_pressed:''
        background_disabled:''
        background_color:(0,0,1,1)
        padding:(10,10)

        Image:
            source:'data/image/delete.png'

            size: (40,40)
            y: self.parent.y + self.parent.height + 20
            x: self.parent.x + 15
            allow_stretch: False
            keep_ratio: True

    Button:
        id:ex_up_btn
        background_normal:''
        background_pressed:''
        background_disabled:''
        background_color:(0,0,0,0)

        Image:
            source:'data/image/arrow_up.png'
            y: self.parent.y + self.parent.height + 20
            x: self.parent.x + 20
            size: '40dp', '40dp'
            allow_stretch: True
    Button:
        id:ex_down_btn
        background_normal:''
        background_pressed:''
        background_disabled:''
        background_color:(0,0,0,0)

        Image:
            source:'data/image/arrow_down.png'
            y: self.parent.y + self.parent.height + 20
            x: self.parent.x + 15
            size: 40, 40
            allow_stretch: True

正如您所看到的,我对尺寸值进行了一些实验但到目前为止没有结果。感谢帮助!

1 个答案:

答案 0 :(得分:1)

我通过将这些按钮添加到嵌套的网格布局并在那里设置固定的列宽来解决它。 我希望防止嵌套布局超过必要但我无法找到更清洁的解决方案。 如果有人知道更好的答案,不要犹豫发布,我会把它打开。

<EditWorkoutExcerciseRow@GridLayout>
    exRepetitionsRound: ex_repetitions_round
    exRepetitionsText: ex_repetitions_ti
    exNameLabel: ex_name_label
    exDeleteBtn:ex_delete_btn
    exUpBtn:ex_up_btn
    exDownBtn: ex_down_btn


    TextInput:
        id: ex_repetitions_round
        multiline:False
        size_hint:(0.2,1)
        padding:(10,10,10,10)

    TextInput:
        size_hint:(0.2,1)
        id: ex_repetitions_ti
        multiline:False
        padding:(10,10,10,10)

    Label:
        size_hint:(0.3,1)
        id: ex_name_label
        text:''

    GridLayout:
        size_hint:(0.3,1)
        cols:3
        rows:1
        col_default_width:50
        col_force_default:True
        spacing:10,10
        Button:
            id:ex_delete_btn
            background_normal:''
            background_pressed:''
            background_disabled:''
            background_color:(0,0,0,0)
            size:50,50
            Image:
                source:'data/image/delete.png'
                size:(50,50)
                y: self.parent.y
                x: self.parent.x
                allow_stretch: True
                keep_ratio: True
        Button:
            id:ex_up_btn
            background_normal:''
            background_pressed:''
            background_disabled:''
            background_color:(0,0,0,0)
            size:50,50
            Image:
                source:'data/image/arrow_up.png'
                size:(50,50)
                y: self.parent.y
                x: self.parent.x
                allow_stretch: True
                keep_ratio: True


        Button:
            id:ex_down_btn
            background_normal:''
            background_pressed:''
            background_disabled:''
            background_color:(0,0,0,0)
            size:50,50
            Image:
                source:'data/image/arrow_down.png'
                y: self.parent.y
                x: self.parent.x
                size:50,50
                allow_stretch: True