简体   繁体   English

如何在 Kivy 的滚动视图顶部创建固定按钮?

[英]How to create a fixed button on top of a scrollview in Kivy?

I am looking for a solution to create a button on top of a scrollview that is not moving with the scroll.我正在寻找一种解决方案来在不随滚动移动的滚动视图顶部创建一个按钮。 Everything that I tried so far creates the button either under the scroll or inside.到目前为止,我尝试的所有操作都在滚动下方或内部创建了按钮。

Here is some of the code.这是一些代码。 I am trying to make the MDRaisedButton to be on top of ScrollView and remain in that position even when a user scrolls.我试图使 MDRaisedButton 位于 ScrollView 之上,并且即使用户滚动也保持在 position 中。

Something like having a tag in HTML with position: fixed;类似于在 HTML 中使用position: fixed;

Python: Python:

from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.uix.list import ImageLeftWidget
from kivymd.uix.list import TwoLineAvatarListItem


sm = ScreenManager()

class MainScreen(Screen):
    def AddToList(self):
        item = TwoLineAvatarListItem(text='@name', id='id')
        item.add_widget(ImageLeftWidget(source='src/avatars/cropped-Avatar-Round.png'))
        self.ids.container.add_widget(item)
        print("+1")

class MainApp(MDApp):
    info = {}
    def build(self):
        self.root_widget = Builder.load_file('test.kv')
        return self.root_widget

if __name__ == '__main__':
    MainApp().run()

Kivy: Kivy:

ScreenManager:
    MainScreen:

<MainScreen>:
    name:'main_screen'

    MDRaisedButton:
        text: "add"
        pos_hint:{"center_x":.5, "center_y":.5}
        index:10
        on_release:
            root.AddToList()
  
    ScrollView:
        do_scroll_x: False
        do_scroll_y: True
        index:5
    
        GridLayout:
            index:5
            cols: 1
            padding: 10
            spacing: 10
            # size_hint: None, None
            size_hint_x: 1
            size_hint_Y: None
            do_scroll_x: False
            id: container

In order to the Button to be "on top" of the ScrollView , you just need to make sure it is drawn after the ScrollView .为了使Button位于ScrollView的“顶部”,您只需要确保它是在ScrollView之后绘制的。 To do that, you can move the Button in the kv to after the ScrollView :为此,您可以将kv中的Button移动到ScrollView之后:

<MainScreen>:
    name:'main_screen'

    ScrollView:
        do_scroll_x: False
        do_scroll_y: True
        index:5
    
        GridLayout:
            index:5
            cols: 1
            padding: 10
            spacing: 10
            # size_hint: None, None
            size_hint_x: 1
            size_hint_y: None
            do_scroll_x: False
            height: self.minimum_height
            id: container
            
    MDRaisedButton:
        text: "add"
        pos_hint:{"center_x":.5, "center_y":.5}
        index:10
        on_release:
            root.AddToList()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM