使用Kivy的RecycleView时自定义小部件的对齐问题

3
我用Kivy的RecycleView创建了一个类似聊天应用的结构,使用自定义小部件(名为'Row')并传递所需的值。如果Row自定义小部件仅包含Label子项,则它可以正常工作。但是,一旦我添加了Image和Button(稍后会解释原因),就会出现奇怪的间距和定位问题,这些问题不应该存在,因为我正在使用BoxLayout,并尝试使用proper size_hint和height技术来使它们看起来正确。
相关KV代码:
<Row@BoxLayout>:
    orientation: 'vertical'
    text: ''
    source: 'res/icon.png'
    buttontext: ''
    Label:
        markup: True
        text_size: self.size
        text: root.text
    Image:
        size: self.size
        source: root.source
        allow_stretch: True
    Button:
        text: root.buttontext #contains a link
        on_press: app.root.stuff(self.text) #opens that link

# Relevant Screen where I am using the RecycleView
<ChatBotScreen>
    BoxLayout:
        ti_userinput: ti_userinput
        orientation: 'vertical'
        ScrollView:
            size_hint_y: 85
            RecycleView:
                id: chatbase_list
                viewclass: 'Row'
                data: []
                RecycleBoxLayout:
                    padding: "15dp", "45dp", "15dp", "15dp"
                    default_size: None, dp(25)
                    default_size_hint: 1, None
                    size_hint_y: None
                    height: self.minimum_height
                    orientation: 'vertical'

        TextInput:
            size_hint_y: None
            height: "40dp"
            id: ti_userinput
            multiline: False
            on_text_validate: root.on_user_enter_text()

由于我的自定义小部件Row扩展了BoxLayout并且我正在使用垂直方向,为什么子小部件(Label、Image和Button)没有重叠?

这是我调用的类和函数,通过它将数据传递到RecycleView中:

    class ChatBotScreen(Screen):
        nickname = StringProperty()
        rvList = []

        def display_bot_output(self, botOutput):
            # Initialize an empty dictionary
            tempDict = {}
            textToDisplay = '' + botOutput

            # Update the dictionary with properties:values and then pass it as a list to RecycleView's data
            tempDict.update({'text':textToDisplay, 'buttontext':'https://google.com'})
            self.rvList.append(tempDict)
            self.ids.chatbase_list.data = self.rvList

我想在屏幕上显示的内容是:

  • 在标签中通过 textToDisplay 变量发送的文本

  • 在标签下方,应该显示一个图像,源可以传递,如果没有传递,则不显示图像

  • 在图像下方,应该显示一个带有链接的按钮,可以通过 buttontext 传递,如果没有传递,则不显示按钮。

我能够呈现所有这些,但是间距和一切都乱了。

以下是屏幕截图:

我首先发送了只有标签文本信息和图像的数据,因此按钮文本为空(它仍然显示),然后我发送另一个数据(第二行小部件),其中包含标签文本信息、图像以及一个按钮文本(谷歌链接)。

all comes on top of each other

任何帮助都将非常感激。谢谢!


1
我找到了罪魁祸首。实际上是在RecycleBoxLayout中的那一行。它设置了default_size: None, dp(25)。那个25dp就是问题所在。 - RhinoFreak
请问您能否解释一下,我遇到了同样的问题。 - Will Jordan
1个回答

0
<ChatBotScreen>
    BoxLayout:
       ti_userinput: ti_userinput
       orientation: 'vertical'
       ScrollView:
       # to have the screen get bigger not lliek what you have
           size: root.size
           RecycleView:
               id: chatbase_list
               viewclass: 'Row'
               data: []
               RecycleBoxLayout:
                    padding: "15dp", "45dp", "15dp", "15dp"
                    default_size: None, dp(25)
                    default_size_hint: 1, None
                    size_hint_y: None
                    height: self.minimum_height
                    # add this so you can scroll
                    row_default_height: 60
                    orientation: 'vertical'

    TextInput:
        size_hint_y: None
        height: "40dp"
        id: ti_userinput
        multiline: False
        on_text_validate: root.on_user_enter_text()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接