如何将小部件绑定到不同屏幕尺寸的中心?
How to tie a widget to the center on different screen sizes?
在我的示例中,有带有数字的板。数字在 400x600 的屏幕尺寸上正确显示。
但是如果您将屏幕尺寸更改为 1200x600,则数字会在很长的距离内逐渐远离彼此:
这是我的做法 (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()
如何让数字居中,不同屏幕尺寸下数字之间的距离始终相同?
尝试将数字图像放在 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
...
在我的示例中,有带有数字的板。数字在 400x600 的屏幕尺寸上正确显示。
但是如果您将屏幕尺寸更改为 1200x600,则数字会在很长的距离内逐渐远离彼此:
这是我的做法 (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()
如何让数字居中,不同屏幕尺寸下数字之间的距离始终相同?
尝试将数字图像放在 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
...