如何在不同的屏幕尺寸上将小部件与中心联系起来?

时间:2016-05-02 07:41:27

标签: kivy kivy-language

在我的例子中,有数字板。这些数字在400x600的屏幕尺寸上正确显示。

enter image description here

但如果您将屏幕尺寸更改为1200x600,那么这些数字会在很远的距离内相互爬行:

enter image description here

我是这样做的(cleanscreen.kv):

#:kivy 1.9.1

<CustomButton@Button>
    text: root.button_text
    size_hint_y: None
    text_size: root.width - 150, root.height
    valign: "middle"
    height: 40


<CleanScreen>
    orientation: "vertical"

    FloatLayout:
        Image:
            size_hint: .52, .52
            pos_hint: {"center_x": .23, "y": .30}
            allow_stretch: True
            source: "6.png"
        Image:
            size_hint: .52, .52
            pos_hint: {"center_x": .43, "y": .30}
            allow_stretch: True
            source: "5.png"
        Image:
            size_hint: .25, .25
            pos_hint: {"center_x": .54, "y": .35}
            allow_stretch: True
            source: "dot.png"
        Image:
            size_hint: .52, .52
            pos_hint: {"center_x": .65, "y": .30}
            allow_stretch: True
            source: "8.png"
        Image:
            size_hint: .18, .18
            pos_hint: {"center_x": .80, "center_y": .75}
            allow_stretch: True
            source: "gb.png"

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

cleanscreen.py:

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

import kivy
kivy.require("1.9.1")

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.config import Config
from kivy.properties import StringProperty

Config.set("graphics", "width", "400")
Config.set("graphics", "height", "600")


class CustomButton(Button):
    button_text = StringProperty("")


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

    def __init__(self, **kvargs):
        super(CleanScreen, self).__init__(**kvargs)
        self.create_custom_button(self.ids.gridlayout_ID)

    def create_custom_button(self, gridlayout_ID):
        for i in range(50):
            gridlayout_ID.add_widget(
                CustomButton(button_text="Button {}".format(i)))

if __name__ in ["__main__", "__android__"]:
    class Test(App):
        def build(self):
            return CleanScreen()


    Test().run()

如何将数字绑定到中心,它们之间的距离在不同的屏幕尺寸上始终相同?

1 个答案:

答案 0 :(得分:1)

尝试将数字图片放在BoxLayout中,并将BoxLayout本身放在中间位置:

FloatLayout:
    BoxLayout:
        orientation: "horizontal"
        #padding: play with this for better padding
        pos_hint: {"center_x": .50, "y": .30}
        Image: #I'm first
        Image: #2nd
        Image: #3rd
        ...