2016-05-02 9 views
1

В моем примере есть доски с цифрами. Цифры отображаются правильно на экране размером 400x600.Как связать виджет с центром на разных размерах экрана?

enter image description here

Но если изменить размер экрана 1200x600, число расползаются друг от друга на большие расстояния:

enter image description here

Вот как я это сделать (cleanscreen.kv) :

#:kivy 1.9.1 

<[email protected]> 
    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

Попробуйте сдачи номера изображения в 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 
     ... 
Смежные вопросы