【问题标题】:How do I automatically Set MDCard height如何自动设置 MDCard 高度
【发布时间】:2021-11-02 09:27:55
【问题描述】:

我想设置mdcard height 使其适合mdlabel,也许不需要我手动设置 mdcard 大小。 请问我该怎么做..在此先感谢..

为了更好地理解这里有一个示例代码:

from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.core.window import Window

Window.size = (300, 530)

KV = """
MDBoxLayout:
    orientation: 'vertical'
    ScrollView:
        MDGridLayout:
            cols: 1
            adaptive_height: True
            padding: '10dp', '15dp'
            spacing: '15dp'

            MDCard:
                orientation: 'vertical'
                size_hint: None, None
                size: 280, 200
                MDLabel:
                    markup: True
                    padding: [15, 15]
                    text:
                        '''
                        [size=25][b]Ford[/b][/size]
                        If mdlabel text becomes too many to fit into the specified mdcard size,\n
                        the text overslaps other things closeby..\n
                        So my question: How do I set MDCard height to automatically\n
                        adjusts accordingly to the height/size of widgets inside of it.\n
                        Thanks in Advance!!
                        '''

            MDCard:
                orientation: 'vertical'
                size_hint: None, None
                size: 280, 200
                MDLabel:
                    markup: True
                    padding: [15, 15]
                    text:
                        '''
                        [size=25][b]Ford[/b][/size]
                        If mdlabel text becomes too many to fit into the specified mdcard size,\n
                        the text overslaps other things closeby..\n
                        So my question: How do I set MDCard height to automatically\n
                        adjusts accordingly to the height/size of widgets inside of it.\n
                        Thanks in Advance!!
                        '''



"""


class Example(MDApp):
    def build(self):
        return Builder.load_string(KV)


Example().run()

就像上面的代码一样,如果 mdlabel 文本变得太多而无法适应指定的 mdcard 大小,则文本会覆盖附近的其他内容。 所以我的问题是:如何将 MDCard 高度设置为根据其中的小部件的高度/大小自动调整。 提前致谢!!

【问题讨论】:

    标签: python-3.x kivy kivymd


    【解决方案1】:

    您可以将MDCardheight 设置为与MDLabelheight 相同,但您必须允许MDLabel 调整到其text 的大小。这是您的 kv 的修改版本:

    MDBoxLayout:
        orientation: 'vertical'
        ScrollView:
            MDGridLayout:
                cols: 1
                adaptive_height: True
                padding: '10dp', '15dp'
                spacing: '15dp'
    
                MDCard:
                    orientation: 'vertical'
                    size_hint: 1, None
                    # size: 280, 200
                    height: label1.height
                    MDLabel:
                        id: label1
                        markup: True
                        padding: [15, 15]
                        text:
                            '''
                            [size=25][b]Ford[/b][/size]
                            If mdlabel text becomes too many to fit into the specified mdcard size,\n
                            the text overslaps other things closeby..\n
                            So my question: How do I set MDCard height to automatically\n
                            adjusts accordingly to the height/size of widgets inside of it.\n
                            Thanks in Advance!!
                            '''
                        size_hint_y: None
                        height: self.texture_size[1] + 2*self.padding[1]
    
                MDCard:
                    orientation: 'vertical'
                    size_hint: 1, None
                    # size: 280, 200
                    height: label2.height
                    MDLabel:
                        id: label2
                        markup: True
                        padding: [15, 15]
                        text:
                            '''
                            [size=25][b]Ford[/b][/size]
                            If mdlabel text becomes too many to fit into the specified mdcard size,\n
                            the text overslaps other things closeby..\n
                            So my question: How do I set MDCard height to automatically\n
                            adjusts accordingly to the height/size of widgets inside of it.\n
                            Thanks in Advance!!
                            '''
                        size_hint_y: None
                        height: self.texture_size[1] + 2*self.padding[1]
    

    【讨论】:

    • 感谢您的努力
    猜你喜欢
    • 1970-01-01
    • 2019-03-11
    • 2013-02-21
    • 2012-10-07
    • 2016-04-25
    • 2015-09-24
    • 1970-01-01
    • 1970-01-01
    • 2021-02-19
    相关资源
    最近更新 更多