如何自动设置 MDCard 高度

How do I automatically Set MDCard height

我想设置 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 高度以根据其中的 height/size 小部件自动调整。 提前致谢!!

您可以将 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]