【问题标题】:Why does it look different on different devices? Kivy为什么它在不同的设备上看起来不一样?基维
【发布时间】:2020-06-20 23:05:46
【问题描述】:

我在PC上写了一个应用程序,然后为android编译,但结果不同。

我需要 python 代码来看看它的真实样子。

是的,我知道不同的设备有不同的像素密度,有人能告诉我如何解决我的问题吗?

main.py:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen

class Test1(Screen):
    pass
class Test2(Screen):
    pass

kvfile = Builder.load_file('main.kv')
class MainApp(App):
    def build(self):
        return kvfile

    def change_screen(self, screen_name):
        screen_manager = self.root.ids['screen_manager']
        screen_manager.current = screen_name

MainApp().run()

main.kv:

<Test1>:
FloatLayout:
    canvas:
        Color:
            rgba: hex('#D8D8D8')
        Rectangle:
            pos: 0, 565
            size: 1500, 2
        Rectangle:
            pos: 0, 48
            size: 1500, 2
    canvas.before:
        Color:
            rgba: hex('#ffffff')
        Rectangle:
            pos: self.pos
            size: self.size
    BoxLayout:
        pos_hint: {'top': 1, 'center_x': .5}
        size_hint_y: .055
        Label:
            text: 'ABC'
            bold: True
            background_color: hex('#ECEDF1')
            color: hex('#2D2E30')
    BoxLayout:
        pos_hint: {'top': .92, 'center_x': .5}
        size_hint_y: .82
        ScrollView:
            GridLayout:
                cols: 1
                size_hint_y: None
                row_default_height: 50
                height: self.minimum_height
                Label:
                    text: '  A'
                    bold: True
                    background_color: hex('#ffffff')
                    color: hex('#2D2E30')
                    text_size: self.size
                    halign: 'left'
                    valign: 'middle'
                Button:
                    text: "  B"
                    bold: True
                    background_normal: ''
                    background_down: ''
                    background_color: hex('#ffffff')
                    color: hex('#2D2E30')
                    text_size: self.size
                    halign: 'left'
                    valign: 'middle'
                Button:
                    text: "  C"
                    bold: True
                    background_normal: ''
                    background_down: ''
                    background_color: hex('#ffffff')
                    color: hex('#2D2E30')
                    text_size: self.size
                    halign: 'left'
                    valign: 'middle'
                Button:
                    text: "  D"
                    bold: True
                    background_normal: ''
                    background_down: ''
                    background_color: hex('#ffffff')
                    color: hex('#2D2E30')
                    text_size: self.size
                    halign: 'left'
                    valign: 'middle'
                Button:
                    text: "  E"
                    bold: True
                    background_normal: ''
                    background_down: ''
                    background_color: hex('#ffffff')
                    color: hex('#2D2E30')
                    text_size: self.size
                    halign: 'left'
                    valign: 'middle'
                Button:
                    text: "  F"
                    bold: True
                    background_normal: ''
                    background_down: ''
                    background_color: hex('#ffffff')
                    color: hex('#2D2E30')
                    text_size: self.size
                    halign: 'left'
                    valign: 'middle'
                Button:
                    text: "  G"
                    bold: True
                    background_normal: ''
                    background_down: ''
                    background_color: hex('#ffffff')
                    color: hex('#2D2E30')
                    text_size: self.size
                    halign: 'left'
                    valign: 'middle'
                Label:
                    text: "  H"
                    bold: True
                    background_color: hex('#ECEDF1')
                    color: hex('#2D2E30')
                    text_size: self.size
                    halign: 'left'
                    valign: 'middle'
                Button:
                    text: "  I"
                    bold: True
                    background_normal: ''
                    background_down: ''
                    background_color: hex('#ffffff')
                    color: hex('#2D2E30')
                    text_size: self.size
                    halign: 'left'
                    valign: 'middle'
                Button:
                    text: "  J"
                    bold: True
                    background_normal: ''
                    background_down: ''
                    background_color: hex('#ffffff')
                    color: hex('#2D2E30')
                    text_size: self.size
                    halign: 'left'
                    valign: 'middle'
                Button:
                    text: "  K"
                    bold: True
                    background_normal: ''
                    background_down: ''
                    background_color: hex('#ffffff')
                    color: hex('#2D2E30')
                    text_size: self.size
                    halign: 'left'
                    valign: 'middle'

    BoxLayout:
        pos_hint: {'top': .08, 'center_x': .5}
        size_hint_y: .08
        Button:
            text: 'A'
            bold: True
            background_normal: ''
            background_down: ''
            background_color: hex('#ECEDF1')
            color: hex('#2D2E30')
            on_press:
                app.change_screen('test1')
        Button:
            text: 'B'
            bold: True
            background_normal: ''
            background_down: ''
            background_color: hex('#ECEDF1')
            color: hex('#2D2E30')
            on_press:
                app.change_screen('test2')

【问题讨论】:

    标签: python linux kivy


    【解决方案1】:

    所以你的基本想法是正确的。实际上是 dp 导致这些字母聚集在一起。

    Kivy 提供了大量的测量单位,您可以使用它们在 kivy 中定位和调整小部件的大小。目前,您的大部分代码仅使用像素大小(您直接指定像素数量),这对于小程序来说已经足够了。但是,一旦您的程序必须跨多个设备进行扩展,这些像素单位就不够了。

    在这种情况下,您可以使用 dp 单位来更好地定位小部件。

    而不是

    GridLayout:
        cols: 1
        size_hint_y: None
        row_default_height: 50
        height: self.minimum_height
    

    添加

    GridLayout:
        cols: 1
        size_hint_y: None
        row_default_height: 50dp
        height: self.minimum_height
    

    即在 row_default_height 的值后面添加 dp。这些值不会是准确的,您可能必须更改它们才能恢复原始位置。 您可以从这里获取更多信息https://kivy.org/doc/stable/api-kivy.metrics.html

    【讨论】:

      猜你喜欢
      • 2012-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多