按钮宽度在PC上是对称的,但在Android Python/Kivy上不是对称的

发布于 2025-02-09 09:34:36 字数 1603 浏览 3 评论 0原文

我创建了一个自定义切换按钮,设置size_hint_x to none放置width。该按钮在PC上是完美的,但是在Android上,按钮的宽度似乎已经重置。

myWidget.kv:

#:import Window kivy.core.window.Window

<MyButton>:
    size_hint_x: None
    width: Window.width/3
    font_name: "path/to/font.ttf"
    md_bg_color: app.theme_cls.bg_dark
    background_down: get_color_from_hex("#2e5871")
    font_color_normal: app.theme_cls.primary_color
    font_color_down: app.theme_cls.primary_light
    allow_no_selection: False

mywidget.py.py

from kivymd.uix.behaviors.toggle_behavior import MDToggleButton
from kivymd.uix.button import MDRectangleFlatButton

class MyButton(MDRectangleFlatButton, MDToggleButton):
    def __init__(self, **kwargs):
        self.background_normal = 1, 1, 1, 1
        super().__init__(**kwargs)

位置我正在使用按钮:

MDRelativeLayout:
    size_hint: 1, None
    height: dp(45)

    MyButton:
        text: "List 1"
        group: "list"
        pos_hint: {"top":1, "left":1}

    MyButton:
        text: "List 2"
        group: "list"
        pos_hint: {"top":1, "center_x":.5}

    MyButton:
        text: "List 3"
        group: "list"
        pos_hint: {"top":1, "right":1}

这是应用程序images

pc上的应用

on android上的应用程序

I created a custom Toggle Button, setting size_hint_x to None to put width. The button is perfect on pc but on android it is as if the width of the button had been reset.

MyWidget.kv:

#:import Window kivy.core.window.Window

<MyButton>:
    size_hint_x: None
    width: Window.width/3
    font_name: "path/to/font.ttf"
    md_bg_color: app.theme_cls.bg_dark
    background_down: get_color_from_hex("#2e5871")
    font_color_normal: app.theme_cls.primary_color
    font_color_down: app.theme_cls.primary_light
    allow_no_selection: False

MyWidget.py

from kivymd.uix.behaviors.toggle_behavior import MDToggleButton
from kivymd.uix.button import MDRectangleFlatButton

class MyButton(MDRectangleFlatButton, MDToggleButton):
    def __init__(self, **kwargs):
        self.background_normal = 1, 1, 1, 1
        super().__init__(**kwargs)

Location I'm using the button:

MDRelativeLayout:
    size_hint: 1, None
    height: dp(45)

    MyButton:
        text: "List 1"
        group: "list"
        pos_hint: {"top":1, "left":1}

    MyButton:
        text: "List 2"
        group: "list"
        pos_hint: {"top":1, "center_x":.5}

    MyButton:
        text: "List 3"
        group: "list"
        pos_hint: {"top":1, "right":1}

Here is the app images

Application on PC

Application on Android

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

他不在意 2025-02-16 09:34:36

通过更改size_hint_x而不是width

The issue was resolved by changing the size_hint_x instead of the width.

Obrigado @ApuCoder

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文