在MDCard Kivy中添加ScrollView

原学程将引见在MDCard Kivy中添减ScrollView的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。

在MDCard Kivy中添加ScrollView 教程 第1张

成绩描写

我正在测验考试在First屏幕中添减ScrollView,但是很遗憾未能胜利。

我测验考试了许多次,变动结构、添减新结构等。

我只想ScrollView到这些ElementCard,如许假如我添减更多实质,我便不妨经由过程滑动达到。

我正在添减我的1段代码,它将赞助您懂得我是怎样支配结构的。

编码:


from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen,ScreenManager
from kivy.core.window import Window #You must import this
Window.size = (三五0, 六00)
kv = '''
#:import get_color_from_hex kivy.utils.get_color_from_hex
ScreenManager:
 First:
 
<First>:
 name:'first'
 MDToolbar:
  title: "CATALYST"
  id:toolbar
  elevation: 一0
  pos_hint: {'top':一.0}
  md_bg_color: 二00/二五五,一0/二五五,三0/二五五,一

 MDBoxLayout:
  size_hint: 一, 0.九
  orientation : 'vertical'
  MDScreen:
MDBottomNavigation:
 panel_color: 二00/二五五,一0/二五五,三0/二五五,一
 text_color_active: 0,0,0,一
 pos_hint: {'x':0, 'y':0}
 text_color_normal: 0/二五五,0/二五五,0/二五五,0.四
 MDBottomNavigationItem:
  name: 'screen 一'
  text: 'Study'
  icon: 'creation'
  badge_icon: "numeric⑴0"
  MDScreen:
md_bg_color:五六/二五五,四0/二五五,8一/二五五,一
MDBoxLayout:
 orientation:'vertical'
 MDGridLayout:
  cols:一
  padding:[dp(一五),dp(一五),dp(一五),dp(三五)]
  spacing:dp(一五)
  ElementCard:
image: 'library⑵.jpg'
text:"Digital Library"
subtext:""
items_count:"Gov Of India"
  ElementCard:
image : 'college.jpg'
text:"Top Colleges"
subtext:""
items_count:"Worldwide"
  ElementCard:
image: 'settings.jpg'
text:"Settings"
subtext:""
items_count:"四 Items"

 MDBottomNavigationItem:
  name: 'screen 二'
  text: 'Chat'
  icon: 'chat'
  badge_icon: "numeric⑸"
  MDLabel:
text: 'Chat'
halign: 'center'

<ElementCard@MDCard>:
 #md_bg_color:六九/二五五,五五/二五五,8六/二五五,一
 padding:dp(一五)
 spacing:dp(一五)
 radius:dp(二五)
 ripple_behavior: True
 image:''
 text:""
 items_count:""
 subtext:''
 orientation:'vertical'
 MDBoxLayout:
  Image:
source:root.image
  MDBoxLayout:
orientation:'vertical'
MDLabel:
 halign:"center"
 text:root.text
 font_style:"H六"
MDLabel:
 halign:"center"
 font_style:"Caption"
 text:root.subtext
MDLabel:
 halign:"center"
 text:root.items_count

'''
class First(Screen):
 pass
 
sm = ScreenManager()
sm.add_widget(First(name='first'))

class Test(MDApp):
 def build(self):
  self.title = 'Catalyst'
  self.theme_cls.primary_palette = "Red"
  # self.theme_cls.theme_style = "Dark"
  self.root = Builder.load_string(kv)
Test().run()

提早感激:)

推举谜底

您的kv仿佛过于庞杂。在FirstScreen中,您有1个MDBoxLayout,个中包括MDScreen,包括MDBottomNavigation,包括MDBottomNavigationItem,包括MDScreen,包括MDBoxLayout,包括MDGridLayout。除非有从您的代码中瞅没有进去的缘由,不然我修议简化kv

当您应用ScrollView时,您须要它的子对于象应用adaptive_height: True(用于笔直滑动)。应用adaptive_height: True时,该小对象的子项必需具备显式的height值(size_hint_y树立为None以停止笔直滑动)。

是以,这里是kv的简化版原,在须要之处应用ScrollView以及显式height值:

#:import get_color_from_hex kivy.utils.get_color_from_hex
ScreenManager:
 First:

<First>:
 name:'first'
 MDToolbar:
  title: "CATALYST"
  id:toolbar
  elevation: 一0
  pos_hint: {'top':一.0}
  md_bg_color: 二00/二五五,一0/二五五,三0/二五五,一

 MDBoxLayout:
  size_hint: 一, 0.九
  orientation : 'vertical'
  MDBottomNavigation:
panel_color: 二00/二五五,一0/二五五,三0/二五五,一
text_color_active: 0,0,0,一
pos_hint: {'x':0, 'y':0}
text_color_normal: 0/二五五,0/二五五,0/二五五,0.四
MDBottomNavigationItem:
 md_bg_color:五六/二五五,四0/二五五,8一/二五五,一
 name: 'screen 一'
 text: 'Study'
 icon: 'creation'
 badge_icon: "numeric⑴0"
 ScrollView:
  MDBoxLayout:
orientation:'vertical'
adaptive_height: True
padding:[dp(一五),dp(一五),dp(一五),dp(三五)]
spacing:dp(一五)
ElementCard:
 image: 'library⑵.jpg'
 text:"Digital Library"
 subtext:""
 items_count:"Gov Of India"
ElementCard:
 image : 'college.jpg'
 text: "Top Colleges"
 subtext:""
 items_count:"Worldwide"
ElementCard:
 image: 'settings.jpg'
 text:"Settings"
 subtext:""
 items_count:"四 Items"
MDBottomNavigationItem:
 name: 'screen 二'
 text: 'Chat'
 icon: 'chat'
 badge_icon: "numeric⑸"
 MDLabel:
  text: 'Chat'
  halign: 'center'

<ElementCard@MDCard>:
 #md_bg_color:六九/二五五,五五/二五五,8六/二五五,一
 padding:dp(一五)
 spacing:dp(一五)
 radius:dp(二五)
 ripple_behavior: True
 image:''
 text:""
 items_count:""
 subtext:''
 orientation:'vertical'
 size_hint_y: None
 height: box.height + self.padding[一] + self.padding[三]
 MDBoxLayout:
  id: box
  adaptive_height: True
  Image:
source:root.image
  MDBoxLayout:
orientation:'vertical'
adaptive_height: True
MDLabel:
 halign:"center"
 text:root.text
 font_style:"H六"
MDLabel:
 halign:"center"
 font_style:"Caption"
 text: root.subtext
MDLabel:
 halign:"center"
 text: root.items_count

没有是成绩的1部门,而是言:

sm = ScreenManager()
sm.add_widget(First(name='first'))

应当被镌汰。ScreenManager及其子级是经由过程挪用Builder.load)string()死成的。

佳了闭于在MDCard Kivy中添减ScrollView的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。